Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

xml - How to unmarshal nested child elements in java with same tag name?

In Java I am able to read XML by adding the values into my POJO. But I am not sure how would I able to do the same with sub-child nodes. I provided my POJO example and the XML sub class node.

XML:

<results>
    <rootNode>
        <node>1336</node>
        <state>CL</state>
        <time>0</time>
        <ip_addresses>
             <ip_address type="DOC">06:56:43.0</ip_address>
             <ip_address type="PE">06:56:43.0</ip_address>
        </ip_addresses>
        <lease_date>2017-01-25</lease_date>
    </rootNode>
</results>

POJO:

@XmlRootElement(name = "rootNode")
@XmlAccessorType (XmlAccessType.FIELD)
public class readingXml
{
    private int id;

    @XmlElement(name = "node")
    private String node;
    @XmlElement(name = "state")
    private String state;

    // How to do the same with sub child-nodes
    @XmlElement(name = "ip_addresses")
    private String ip_addresses;
    // What to do here?

    @XmlElement(name = "lease_date")
    private String lease_date;

    // Getters and setters for all above
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You need to put something like this:

@XmlElement(name = "ip_addresses")
private IPAddresses ipAddresses;

and IPAddresses POJO class:

@XmlRootElement(name = "ip_addresses")
@XmlAccessorType(XmlAccessType.FIELD)
public class IPAddresses implements Serializable {
    private final static long serialVersionUID = 1L;

    @XmlElement(name = "ip_address")
    private List<IPAddress> ipAddresses;

    public List<IPAddress> getIpAddresses() {
        return ipAddresses;
    }
    public void setIpAddresses(List<IPAddress> ipAddresses) {
        this.ipAddresses = ipAddresses;
    }
}

Where IPAddress is another POJO class that describes the structure of the individual element.

@XmlRootElement(name = "ip_address")
@XmlAccessorType(XmlAccessType.FIELD)
public class IPAddress implements Serializable {
    private final static long serialVersionUID = 1L;
    @XmlValue
    protected String content;
    @XmlAttribute(name = "type")
    protected String type;

    public void setContent(String content) {
        this.content = content;
    }
    public String getContent() {
        return content;
    }

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

EDIT To print them do something like this:

for (IPAddress ipAddress in custinfo.getIpAddresses().getIpAddresses()) {
    System.out.println("value: " + ipAddress.getContent());
    System.out.println("type: " + ipAddress.getType());
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...