gpt4 book ai didi

Java实现设计问题

转载 作者:行者123 更新时间:2023-11-30 04:51:03 24 4
gpt4 key购买 nike

我有 XML 结构。

<cars>
<car name = "BMW" engine="2.5"/>
<car name = "Lexus" engine="4.5"/>
<car name = "VW" engine="1.4"/>
<car name = "Honda" engine="2.0"/>
</cars>

我为每种车型都有 Java 类。

public class BMW extends Car{

public BMW(){


}


}

如何设计 main() 类来解析此 XML 并自动调用所需 Car 的构造函数。假设我得到一个节点 <.car name = "BMW"engine="2.5"/> 这意味着我想调用 BMW 构造函数创建一个 BMW 对象并将所有内容存储到 List<.Car> 中。

感谢您的任何提示! :)

最佳答案

您可以使用任何 JAXB (JSR-222) 来映射此用例通过利用 XmlAdapter:

车载适配器

在您的示例中,您使用自定义节点作为继承指示器。使用标准 JAXB API,我们可以使用 XmlAdapter 来映射此用例。 XmlAdapter 从域对象转换为更易于 JAXB 实现(Metro、MOXy、JaxMe 等)映射的对象。

package forum9812778;

import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.adapters.XmlAdapter;

public class CarAdapter extends XmlAdapter<CarAdapter.AdaptedCar, Car> {

@Override
public AdaptedCar marshal(Car car) throws Exception {
AdaptedCar adaptedCar = new AdaptedCar();
adaptedCar.name = car.getClass().getSimpleName();
adaptedCar.engine = car.getEngine();
return adaptedCar;
}

@Override
public Car unmarshal(AdaptedCar adaptedCar) throws Exception {
Car car;
if("BMW".equals(adaptedCar.name)) {
car = new BMW();
} else if("Lexus".equals(adaptedCar.name)) {
car = new Lexus();
} else if("VW".equals(adaptedCar.name)) {
car = new VW();
} else if("Honda".equals(adaptedCar.name)) {
car = new Honda();
} else {
return null;
}
car.setEngine(adaptedCar.engine);
return car;
}

public static class AdaptedCar {
@XmlAttribute
public String name;

@XmlAttribute
public String engine;
}

}

汽车

@XmlJavaTypeAdapter 注释用于将 XmlAdapterCar 类关联:

package forum9812778;

import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;

@XmlRootElement
@XmlJavaTypeAdapter(CarAdapter.class)
public class Car {

private String engine;

public String getEngine() {
return engine;
}

public void setEngine(String engine) {
this.engine = engine;
}

}

宝马

下面是其中一个子类的示例。

package forum9812778;

public class BMW extends Car {

}

汽车

我们需要一个对象来表示树中的根节点。我们将定义 Cars 类来充当此角色:

package forum9812778;

import java.util.List;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class Cars {

private List<Car> car;

public List<Car> getCar() {
return car;
}

public void setCar(List<Car> car) {
this.car = car;
}

}

演示

package forum9812778;

import java.io.File;
import javax.xml.bind.*;

public class Demo {

public static void main(String[] args) throws Exception {
JAXBContext jc = JAXBContext.newInstance(Cars.class);

File xml = new File("src/forum9812778/input.xml");
Unmarshaller unmarshaller = jc.createUnmarshaller();
Cars cars = (Cars) unmarshaller.unmarshal(xml);

for(Car car : cars.getCar()) {
System.out.println(car.getClass());
}

Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(cars, System.out);
}

}

输出

class forum9812778.BMW
class forum9812778.Lexus
class forum9812778.VW
class forum9812778.Honda
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<cars>
<car engine="2.5" name="BMW"/>
<car engine="4.5" name="Lexus"/>
<car engine="1.4" name="VW"/>
<car engine="2.0" name="Honda"/>
</cars>

了解更多信息

关于Java实现设计问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9812778/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com