简体   繁体   中英

Convert complex XML to Java object

i have a xml and i want to save into a string the sub xml formed by the child of a specific tag. this is a xml example:

    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<SampleDTO>
    <id>1</id>
    <someList>
        <someObject>
            <amount>32</amount>
            <id>1</id>
            <someDescription>I am a description</someDescription>
        </someObject>
        <someObject>
            <amount>66</amount>
            <id>2</id>
            <someDescription>I am another description</someDescription>
        </someObject>
        <someObject>
            <amount>78</amount>
            <id>13</id>
            <someDescription>Guess what? I am a description</someDescription>
        </someObject>
    </someList>
    <otherList>
        <otherObject>
            <flag>true</flag>
            <id>1</id>
            <otherDescription>Oh nice, a description</otherDescription>
        </otherObject>
    </otherList>
</SampleDTO>

i want , passing for example "someList" , to save into a String the sub-xml element and value, because next i deserialize it into a java object

use the JAXB unmarshaller for converting xml document into java objects. firstly add JAXB dependency into your project's classpath. for more info

SampleDTO.java

@XmlRootElement
public class SampleDTO {
    private String id;
    private List<SomeList> someList;
    private List<OtherList> otherList;

    @XmlElement
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    @XmlElement
    public List<SomeList> getSomeList() {
        return someList;
    }
    public void setSomeList(List<SomeList> someList) {
        this.someList = someList;
    }
    @XmlElement
    public List<OtherList> getOtherList() {
        return otherList;
    }
    public void setOtherList(List<OtherList> otherList) {
        this.otherList = otherList;
    }
}

SomeList.java

@XmlRootElement
public class SomeList {
    private List<SomeObject> someObject;

    @XmlElement
    public List<SomeObject> getSomeObject() {
        return someObject;
    }
    public void setSomeObject(List<SomeObject> someObject) {
        this.someObject = someObject;
    }
}

OtherList.java

@XmlRootElement
public class OtherList {
    private List<OtherObject> otherObject;

    @XmlElement
    public List<OtherObject> getOtherObject() {
        return otherObject;
    }
    public void setOtherObject(List<OtherObject> otherObject) {
        this.otherObject = otherObject;
    }
}

SomeObject.java

@XmlRootElement
public class SomeObject {
    private String amount;
    private String id;
    private String someDescription;

    @XmlElement
    public String getAmount() {
        return amount;
    }
    public void setAmount(String amount) {
        this.amount = amount;
    }
    @XmlElement
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    @XmlElement
    public String getSomeDescription() {
        return someDescription;
    }
    public void setSomeDescription(String someDescription) {
        this.someDescription = someDescription;
    }
}

OtherObject.java

@XmlRootElement
public class OtherObject {
    private String flag;
    private String id;
    private String otherDescription;

    @XmlElement
    public String getFlag() {
        return flag;
    }
    public void setFlag(String flag) {
        this.flag = flag;
    }
    @XmlElement
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    @XmlElement
    public String getOtherDescription() {
        return otherDescription;
    }
    public void setOtherDescription(String otherDescription) {
        this.otherDescription = otherDescription;
    }
}

Unmarshalling with JAXB

public class Main {
    public static void main(String[] args) {  
     try {
        File file = new File("file.xml");  
        JAXBContext jaxbContext = JAXBContext.newInstance(SampleDTO.class);  
        Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();  
        SampleDTO sampleDTO= (SampleDTO) jaxbUnmarshaller.unmarshal(file);  
      } catch (JAXBException e) {  
        e.printStackTrace();  
      }  
    }  
}

Your java class/object should have at least these 3 instance vars :

private int amount
private int id
private String description

Then use some xml parsing library (eg jdom2 ), and for each <someObject> tag you iterate through, initialize a new object of your class and assign to it the values parsed from the xml (amount / id / description) , and add each newly created object in a List or array etc..

There are many open-source XML processing packages available. I like Jackson. Here is a link to a Baeldung Article about Jackson XML

The summary is this:

  1. Add a Jackson dependency to your POM.
  2. Create an object structure that represents your xml structure.
  3. Create an XmlMapper.
  4. Use the XmlMapper.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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