使用JAXB解组

时间:2014-02-11 12:15:21

标签: java xml jaxb unmarshalling

我需要将XML字符串解组成我项目中已存在的类,但我无法弄清楚如何将XML的某个部分解组为对象列表。让我用一些代码解释一下:

我有这个XML:

<user>
  <id>123</id>
  <name>John Doe</name>
  <vaddresses>
    <address>
      <street>Street XYZ</street>
    </address>
    <address>
      <street>Street ABC</street>
    </address>
  </vaddresses>
</user>

我有这些课程:

  • 用户
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "User", propOrder = {"id", "name", "addresses"})
public class User {

    @XmlElement
    private int id;

    @XmlElement
    private String name;

    @XmlElement
    private List<Address> addresses;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public List<Address> getAddresses() {
        return addresses;
    }

    public void setAddresses(List<Address> addresses) {
        this.addresses = addresses;
    }
  • 地址
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "Address", propOrder = {"street"})
public class Address {

    @XmlElement
    private String street;

    public String getStreet() {
        return street;
    }

    public void setStreet(String street) {
        this.street = street;
    }
}

当我尝试将XML解组为这些类时,正确处理字段 id 名称,但地址<不会发生相同的情况/强>:

  • 输出
User: 
    Id: 123
    Nome: John Doe
    Addresses: null

我该如何解决这个问题?我可以使用一些JAXB注释吗?如何?或者我需要创建某种类型的XmlAdapter? (我试过这个,但没有成功......)

3 个答案:

答案 0 :(得分:1)

我认为你必须改变私有财产地址的类型:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "User", propOrder = {"id", "name", "vaddresses"})
public class User {

    @XmlElement
    private int id;

    @XmlElement
    private String name;

    @XmlElement
    private VAddress vaddresses;

    //...
 }

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "VAddress", propOrder = {"addresses"})
public class VAddress {

    @XmlElement
    private List<Address> addresses;

    //...
 }

也许您也可以尝试注释@XmlElementWrapper。

此致

答案 1 :(得分:1)

您可以利用@XmlElementWrapper在您的收藏集周围添加分组元素:

@XmlElementWrapper(name="vaddresses")
@XmlElement(name="address")
private List<Address> addresses;

注意

您正在添加比必要更多的注释。 JAXB是异常配置,因此您只需要注释XML表示与默认值不同的位置。

答案 2 :(得分:0)

User.java

package generated;

import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
    "id",
    "name",
    "vaddresses"
})
@XmlRootElement(name = "user")
public class User {

    protected int id;
    @XmlElement(required = true)
    protected String name;
    @XmlElement(required = true)
    protected User.Vaddresses vaddresses;

    public int getId() {
        return id;
    }


    public void setId(int value) {
        this.id = value;
    }

    public String getName() {
        return name;
    }


    public void setName(String value) {
        this.name = value;
    }


    public User.Vaddresses getVaddresses() {
        return vaddresses;
    }

    public void setVaddresses(User.Vaddresses value) {
        this.vaddresses = value;
    }


    @XmlAccessorType(XmlAccessType.FIELD)
    @XmlType(name = "", propOrder = {
        "address"
    })
    public static class Vaddresses {

        @XmlElement(required = true)
        protected List<User.Vaddresses.Address> address;

        public List<User.Vaddresses.Address> getAddress() {
            if (address == null) {
                address = new ArrayList<User.Vaddresses.Address>();
            }
            return this.address;
        }


        @XmlAccessorType(XmlAccessType.FIELD)
        @XmlType(name = "", propOrder = {
            "street"
        })
        public static class Address {

            @XmlElement(required = true)
            protected String street;


            public String getStreet() {
                return street;
            }

            public void setStreet(String value) {
                this.street = value;
            }

        }

    }

}

JaxBExample.java

package generated;
import generated.User.Vaddresses.Address;

import java.io.File;

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

public class JAXBExample {
    public static void main(String[] args) {

     try {

        File file = new File("D:\\StackOverFlow\\JAXBTest\\file.xml");
        JAXBContext jaxbContext = JAXBContext.newInstance(User.class);

        Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
        User user = (User) jaxbUnmarshaller.unmarshal(file);
        System.out.println("ID::"+user.getId());
        System.out.println("Name::"+user.getName());
        System.out.print("Addresses::");
        for(Address address:user.getVaddresses().getAddress())
        {
            System.out.println(address.getStreet());
        }


      } catch (JAXBException e) {
        e.printStackTrace();
      }

    }
}

输出: -

ID :: 123 姓名:: John Doe 地址:: Street XYZ 街头ABC