Java XML属性解组的空值

Java XML属性解组的空值,java,jaxb,unmarshalling,Java,Jaxb,Unmarshalling,这是我的xml文件,在打印解组和rest所有值时,它将为type和currency返回空值。我在这里使用了解组,指定了所有的父级和子级POOJ,最后我的主方法调用了解组函数 1) Vehicle.xml <?xml version="1.0" encoding="UTF-8"?> <Vehicle> <car> <manufacturer>Maruti</manufacturer> <cost cur

这是我的xml文件,在打印解组和rest所有值时,它将为typecurrency返回空值。我在这里使用了解组,指定了所有的父级和子级POOJ,最后我的主方法调用了解组函数
1) Vehicle.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>
4) 用于解编的Fie

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]]]
试试这个:

@XmlAttribute(name = "cost currency")
public String getCurrency() {
    return currency;
}

一种常见的方法是添加一个额外的类来处理属性+值。如果您不想在使用
汽车时进行额外的间接操作
。您可以向其添加快捷方式getter

public class Car {
    // .....

    private Money cost;

    public Money getCost() {
        return cost;
    }

    public void setCost(Money cost) {
        this.cost = cost;
    }
    /* Optional shortcut getter */
   public String getCurrency(){
     if(getCost()==null){
        return null;
     }
     return getCost().getCurrency();
   }
}

public static class Money {
    private String currency;
    private int amount;

    @XmlAttribute
    public String getCurrency() {
        return currency;
    }

    public void setCurrency(String currency) {
        this.currency = currency;
    }

    @XmlValue
    public int getAmount() {
        return amount;
    }

    public void setAmount(int cost) {
        this.amount = cost;
    }
}

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

以下是如何映射属性(为简单起见,使用publci属性):


今后请不要在标题中使用所有大写字母。
@XmlAttribute(name = "cost currency")
public String getCurrency() {
    return currency;
}
@XmlAttribute(name = "name type")
public String getType() {
    return type;
}
public class Car {
    // .....

    private Money cost;

    public Money getCost() {
        return cost;
    }

    public void setCost(Money cost) {
        this.cost = cost;
    }
    /* Optional shortcut getter */
   public String getCurrency(){
     if(getCost()==null){
        return null;
     }
     return getCost().getCurrency();
   }
}

public static class Money {
    private String currency;
    private int amount;

    @XmlAttribute
    public String getCurrency() {
        return currency;
    }

    public void setCurrency(String currency) {
        this.currency = currency;
    }

    @XmlValue
    public int getAmount() {
        return amount;
    }

    public void setAmount(int cost) {
        this.amount = cost;
    }
}
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 + "]";
    }

}