简体   繁体   English

与JAXB解组

[英]Unmarshal with JAXB

I need to unmarshal a XML string into a class that already exists in my project, but I can't figure out how to unmarshal a certain part of the XML to a list of objects. 我需要将XML字符串解组到项目中已经存在的类中,但是我无法弄清楚如何将XML的特定部分解组到对象列表中。 Let me explain with some code: 让我用一些代码解释一下:

I have this 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>

And I have these classes: 我有这些课程:

  • 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;
    }
  • Address 地址
@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;
    }
}

When I try to unmarshal the XML into these classes, fields id and name are processed correctly, but the same doesn't occur with the addresses : 当我尝试将XML解组到这些类中时,字段idname的处理正确,但是地址却没有相同:

  • Output 产量
User: 
    Id: 123
    Nome: John Doe
    Addresses: null

How do I solve this problem? 我该如何解决这个问题? Is there some JAXB annotation that I can use? 我可以使用一些JAXB注释吗? And how? 如何? Or do I need to create some type of XmlAdapter? 还是我需要创建某种类型的XmlAdapter? (I've tried this one but without success...) (我已经尝试过了,但是没有成功...)

I think you have to change the type of the private property addresses: 我认为您必须更改私有财产地址的类型:

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

    //...
 }

Maybe you can also try the annotation @XmlElementWrapper. 也许您也可以尝试使用@XmlElementWrapper注释。

Regards, 问候,

You can leverage @XmlElementWrapper to add a grouping element around your collection: 您可以利用@XmlElementWrapper在您的集合周围添加分组元素:

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

Note 注意

You are adding more annotations than are necessary. 您添加的注释超出了必要。 JAXB is configuration by exception so you only need to annotate where you want the XML representation to differ from the default. JAXB是例外配置,因此您只需注释要XML表示形式与默认设置不同的位置。

User.java 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 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();
      }

    }
}

output:-- 输出: -

ID::123 Name::John Doe Addresses::Street XYZ Street ABC ID :: 123名称:: John Doe地址:: Street XYZ Street ABC

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM