简体   繁体   中英

How to write pojo class as a data model for complex xml structure

I'm parsing an XML document which has a fairly complex data structure.

Example:

<Companies>
    <LISTID>6353HHJDLS628JNHJ6</LISTID>

    <Company>
        <ID>123ABC</ID>
        <Value>True</Value>
        <Order>
            <Text>Because </Text>
            <ListOfReasons>
                <InputName>
                    <Text>your company did not meet requirements</Text>
                </InputName>
                <Text>, </Text>
                <InputName>
                    <Text>your company was not listed as qualified</Text>
                </InputName>
                <Text> etc...</Text>
            </ListOfReasons>
        </Order>
    </Company>

    <Company>
        <ID>123DEF</ID>
        <Value>False</Value>
        <Order>
            <Text>We can't get any more details on </Text>
            <NodeName>
                <Text>neither your company or the entity in question</Text>
            </NodeName>
            <Text> right now.</Text>
        </Order>    
    </Company>

</Companies>
</root>

How should I model my pojo class? To me it seems it should have nested or inner classes. I am not sure how this would look like

I know all about JaxB but I don't really know how to use it and unless there is some easy way to implement it, I prefer writing a pojo, because I understand it.

I'm DOM parsing and I'd like to represent it in Java objects. That is the purpose for writing this model. Can anyone give me an example data model class using the XML I've shown. Any help or assistance would be much appreciated.

有一个很棒的工具可以帮助您从这里开始。

In your case there are classes like this:

@XmlRootElement
class Companies {
  private String LISTID;

  private List<Company> companies;

  @XmlElement(name = "company")
  public void setCompany(List<Company> companies) {
  this.companies = companies;
  }

  @XmlElement
  public void setLISTID(String LISTID) {
   this.LISTID = LISTID; 
 }
  /** Others standard POJO Methods */
}

@XmlRootElement
public class Company {
  private String id;
  private String value;
  private List<Order> orders;

  /** Like in the previous example**
}

With the tool showed above, your work will only to decorate classes with @Xml* annotations.

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