gpt4 book ai didi

java - XML 属性解码的空值

转载 作者:行者123 更新时间:2023-11-30 12:02:55 25 4
gpt4 key购买 nike

这是我的 xml 文件,它在解码时为 typecurrency 返回空值,其余的所有值都被打印出来。我在这里使用了 unmarshalling 并且指定了所有 Parent 和 Child POOJ,最后我的 Main 方法调用了 unmarshall 函数
1) 车辆.xml

<?xml version="1.0" encoding="UTF-8"?>
<Vehicle>
<car>
<manufacturer>Maruti</manufacturer>
<cost currency="INR">675000</cost>
<name type="sedan">Ciaz</name>
<fuelType>Petrol</fuelType>
<driverType>Manual</driverType>
</car>
<car>
<manufacturer>Maruti</manufacturer>
<cost currency="INR">575000</cost>
<name type="sedan">Dezire</name>
<fuelType>Petrol</fuelType>
<driverType>Manual</driverType>
</car>
</Vehicle>

各自的文件如下2) 车辆.java

package 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;
}

@Override
public String toString() {
return "Vehicle[ Car=" + car + "]";
}

}

3) POJO Car.java 的 child

package jaxb;

import javax.xml.bind.annotation.XmlAttribute;
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 String currency;


@XmlAttribute
public String getCurrency() {
return currency;
}
public void setCurrency(String currency) {
this.currency = currency;
}

@XmlAttribute
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}


private String type;

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;
}

@XmlElement
public int getCost() {
return cost;
}
public void setCost(int cost) {
this.cost = cost;
}


@Override
public String toString() {
return "Car [name=" + name + ", fuelType=" + fuelType + ", cost=" + cost+",driverType="+driverType +",currency="+currency+ " , type="+type +"]";
}


}

4) 用于解码的文件

package jaxb;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;

public class VehicleJxb {

public void unmarhalling() {

try {
JAXBContext jaxbContext = JAXBContext.newInstance(Vehicle.class);

Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();

Vehicle vehicle = (Vehicle) jaxbUnmarshaller.unmarshal(new File("src\\main\\java\\Data\\Vehicle.xml"));

System.out.println(vehicle);

} catch (JAXBException e) {

e.printStackTrace();
}

}

}

5)最终输出

Vehicle[ Car=[Car [name=Ciaz, fuelType=Petrol, cost=675000,driverType=Manual,currency=null , type=null], Car [name=Dezire, fuelType=Petrol, cost=575000,driverType=Manual,currency=null , type=null]]]

最佳答案

使用 JAXB,您只能在同一级别映射属性。要在嵌入式元素上映射属性,您应该为这些元素使用单独的类。

下面是映射属性的方法(为简单起见使用 publci 属性):

import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlValue;

@XmlRootElement(name = "Car")
public class Car {

public static class Cost {

@XmlValue
public String value;

@XmlAttribute
public String currency;

@Override
public String toString() {
return "Cost[value=" + value + ", currency=" + currency + "]";
}

}

public static class Name {

@XmlValue
public String value;

@XmlAttribute
public String type;

@Override
public String toString() {
return "Name[value=" + value + ", type=" + type + "]";
}

}

private String manufacturer;
private Name name;
private String driverType;
private String fuelType;

@XmlAttribute
public String getType() {
return type;
}

public void setType(String type) {
this.type = type;
}

private String type;

private Cost cost;

@XmlElement
public String getManufacturer() {
return manufacturer;
}

public void setManufacturer(String manufacturer) {
this.manufacturer = manufacturer;
}

@XmlElement
public Name getName() {
return name;
}

public void setName(Name 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;
}

@XmlElement
public Cost getCost() {
return cost;
}

public void setCost(Cost cost) {
this.cost = cost;
}

@Override
public String toString() {
return "Car [name=" + name + ", fuelType=" + fuelType + ", cost=" + cost + ",driverType=" + driverType + "]";
}

}

关于java - XML 属性解码的空值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58122636/

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