简体   繁体   English

如何将Pojo类编写为复杂xml结构的数据模型

[英]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. 我正在解析一个具有相当复杂的数据结构的XML文档。

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? 我应该如何模拟我的pojo课? 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. 我了解JaxB的全部知识,但我真的不知道如何使用它,除非有一些简单的实现方法,否则我更喜欢编写pojo,因为我了解它。

I'm DOM parsing and I'd like to represent it in Java objects. 我正在DOM解析中,我想用Java对象表示它。 That is the purpose for writing this model. 这就是编写此模型的目的。 Can anyone give me an example data model class using the XML I've shown. 谁能使用我展示的XML给我一个示例数据模型类。 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. 使用上面显示的工具,您的工作将只用@Xml*注释修饰类。

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

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