0

I have such xml, and I want to read it to POJO. How can I create POJO without read the attributes: Admin, time, dirtyId ..? Which json annotations should I add (the code written in Java)?

<response status="success" code="19">
    <result total-count="1" count="1">
        <rules admin="admin" dirtyId="39" time="2017/05/28 11:35:18">
        <entry name="Shared" admin="admin" dirtyId="33" time="2017/04/03 15:24:03">
                <source admin="admin" dirtyId="33" time="2017/04/03 15:24:03">
                    <member admin="admin" dirtyId="33" time="2017/04/03 15:24:03">ip-10.30.14.14</member>
                </source>
                <destination admin="admin" dirtyId="33" time="2017/04/03 15:24:03">
                    <member admin="admin" dirtyId="33" time="2017/04/03 15:24:03">ip-10.30.14.25</member>
                </destination>
            </entry>
</rules>
    </result>

This is the POJO represent the entry in xml:

public class PanoramaRule {
    private PanoramaRuleOptions option;
    private String name;
    private List<String> to;
    private List<String> from;
    private List<String> source;
    private List<String> destination;
    @JsonProperty("source-user")
    private List<String> sourceUser;
    private List<String> category;
    private List<String> application;
    private List<String> service;
    @JsonProperty("hip-profiles")
    private List<String> hipProfiles;
    private String action;
    private String description;
    private Boolean disabled;

    public void setDisabled(String disabled) {
        this.disabled = "yes".equals(disabled);
    }
}

Thanks, Michal

Michal_Ha
  • 1
  • 2
  • Have you check these links https://stackoverflow.com/questions/14789302/parse-xml-to-java-pojo-in-efficient-way https://stackoverflow.com/questions/1651924/simple-java-xml-to-pojo-mapping-binding – Syeda Zunaira Jun 01 '17 at 05:00

1 Answers1

0

I personally believe there is no easy way to map this xml to your class. The easiest way would be to read the xml and put the attributes manually into your class. But you propably should redesign your class as it is not really object oriented. The different tags should obviously be individual classes. You should not just put attributes into lists instead of using classes.

To read an xml file, jaxb is very often an easy way. I've made a simple example with a small portion of your xml and my own classes. You will figure out the rest. To use this example you need jaxb on your classpath.

The small xml:

<response status="success" code="19">
    <result total-count="1" count="1">
    </result>
</response>

I split the values into different classes:

 public class Result {

    private int totalCount;
    private int count;

    /* The name Attribute defines the tag name in the xml */
    @XmlAttribute(name = "total-count")
    public int getTotalCount() {
        return totalCount;
    }

    public void setTotalCount(int totalCount) {
        this.totalCount = totalCount;
    }

    @XmlAttribute
    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }
}

and

/* Marker that this can be the first tag in your xml */
@XmlRootElement
public class Response {

    private String status;

    private int code;

    private Result result;

    /* <response status="success" ... is an attribute */
    @XmlAttribute
    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    @XmlAttribute
    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    /* <result> is an element which can have it's own attributes */
    @XmlElement
    public Result getResult() {
        return result;
    }

    public void setResult(Result result) {
        this.result = result;
    }
}

You will propably need a few more classes to read your original xml. Sample Code for reading the file:

public static void main(String[] args) throws Exception {

    final JAXBContext instance = JAXBContext.newInstance(Response.class);
    final Unmarshaller unmarshaller = instance.createUnmarshaller();

    final Response result = (Response) unmarshaller.unmarshal(new File("sample.xml"));

    System.out.println(result);
}

This should give you your object to work with.

Dennux
  • 240
  • 1
  • 8