您好,我想使用 maven 依赖项使用解组来获取嵌套 xml 的值,但最终输出返回空值。我在 maven 项目中使用了 3 个包,并且在获取值后,vehicle.xml 包含 car 的值必须将它们插入到access数据库中我的 XML 文件---> Vehicle.xml<?xml version="1.0" encoding="UTF-8"?><Vehicle> <Car> <manufacturer>Maruti</manufacturer> <cost>675000</cost> <name>Ciaz</name> <fueType>Petrol</fueType> <driverTye>Manual</driverTye> </Car> <Car> <manufacturer>Maruti</manufacturer> <cost>575000</cost> <name>Dezire</name> <fueType>Petrol</fueType> <driverTye>Manual</driverTye> </Car></Vehicle>歌唱班Vehicle.javapackage jaxb;import java.util.List;import javax.xml.bind.annotation.XmlElement;import javax.xml.bind.annotation.XmlRootElement;@XmlRootElement(name="Vehicle")public class Vehicle { @XmlElement private List<Car> car; public List<Car> getCar() { return car; } /* * public Vehicle(List<Car> car) { super(); this.car = car; } */ @Override public String toString() { return "Vehicle[ Car="+car+"]"; }}Car.java(这是子 POJO)package jaxb;import javax.xml.bind.annotation.XmlElement;import javax.xml.bind.annotation.XmlRootElement;@XmlRootElement(name="Car")public class Car { private String manufacturer; private String name; private String driverType; private String fuelType; private int cost; @XmlElement public String getManufacturer() { return manufacturer; } public void setManufacturer(String manufacturer) { this.manufacturer = manufacturer; } @XmlElement public String getName() { return name; } public void setName(String name) { this.name = name; } @XmlElement public String getDriverType() { return driverType; } public void setDriverType(String driverType) { this.driverType = driverType; } @XmlElement public String getFuelType() { return fuelType; } public void setFuelType(String fuelType) { this.fuelType = fuelType; }
1 回答
长风秋雁
TA贡献1757条经验 获得超7个赞
Unmarshaller区分大小写。在您的 pojo 中,您有变量名称car,但在 XML 中您有Car. 改成下面的就可以了。
<?xml version="1.0" encoding="UTF-8"?>
<Vehicle>
<car>
<manufacturer>Maruti</manufacturer>
<cost>675000</cost>
<name>Ciaz</name>
<fueType>Petrol</fueType>
<driverTye>Manual</driverTye>
</car>
<car>
<manufacturer>Maruti</manufacturer>
<cost>575000</cost>
<name>Dezire</name>
<fueType>Petrol</fueType>
<driverTye>Manual</driverTye>
</car>
</Vehicle>
或者您需要明确提及这一点。
@XmlElement(name = "Car")
private List<Car> car;
添加回答
举报
0/150
提交
取消