简体   繁体   English

使用jaxb解析嵌套的同名元素

[英]parsing nested same name elements using jaxb

i've searched alot for a solution but unfortunately i didn't find any that could solve my problem. 我已经搜索了很多解决方案,但是不幸的是我没有找到任何可以解决我问题的方法。

I am having a huge xml for an e-learning platform. 我在一个电子学习平台上有一个巨大的xml。 in this xml i have nested element with the same name. 在此xml中,我具有相同名称的嵌套元素。

such as: 如:

<organizations>
<organization identifier="" structure="">
  <title>TITLE</title>
  <item identifier="ITEM_0" identifierref="55555555" image="" isvisible="1" pagetype="" parameters="">
    <title>Welcome</title>
  </item>
  <item identifier="ITEM_456" identifierref="6666666" image="" isvisible="1" pagetype="" parameters="">
    <title>TITLE1</title>
    <item identifier="ITEM_457" identifierref="77777777" image="" isvisible="1" pagetype="" parameters="">
      <title>TITLE2</title>
      <item identifier="ITEM_219" identifierref="88888888" image="" isvisible="1" pagetype="" parameters="">
        <title>TITLE3</title>
      </item>
      <item identifier="ITEM_73" identifierref="99999999" image="" isvisible="1" pagetype="" parameters="">
        <title>TITLE4</title>
      </item>
      <item identifier="ITEM_74" identifierref="000000000" image="" isvisible="1" pagetype="" parameters="">
        <title>TITLE5</title>
      </item> </item>

As we can see the are several "item"s in "item" and whenever i try to callback the items i get only the first "parent" item. 如我们所见,“项目”中有多个“项目”,每当我尝试回调项目时,我只会得到第一个“父”项目。

Here is my Item java class: 这是我的Item java类:

@XmlElementRef(name="item")
public List<Item> items = new ArrayList<Item>();
@XmlAttribute(name = "identifier", required = true)
@XmlJavaTypeAdapter(CollapsedStringAdapter.class)
@XmlSchemaType(name = "NCName")
protected String identifier;
@XmlAttribute(name = "identifierref", required = false)
@XmlJavaTypeAdapter(CollapsedStringAdapter.class)
@XmlSchemaType(name = "NCName")
protected String identifierref;
@XmlAttribute(name = "isvisible", required = false)
protected boolean isvisible;

For example, whenever i call a title of any of the child items i always the main parent title "Welcome" ! 例如,每当我调用任何子项的标题时,我始终都是父项的主要标题“ Welcome”! it means i can't get into the recursive. 这意味着我无法进入递归。 Although my method is totally right after a long time of debugging... whenever i call getItems() i get []. 尽管经过长时间的调试,我的方法是完全正确的……每当我调用getItems()时,我都会得到[]。 any help? 有什么帮助吗?

May be this could help: JAXB endless data structure, recursive binding? 可能对您有帮助: JAXB无休止的数据结构,递归绑定吗?

@XmlAccessorType(XmlAccessType.FIELD) // add this to avoid FIELD/METHOD conflicts
public class Item {
    private int id;
    private String name;

    @XmlElement(name="item")//There is no need for XmlElementRef 
    private List<Item> items = new ArrayList<Item>();

    @XmlAttribute(name = "identifier", required = true)
    @XmlJavaTypeAdapter(CollapsedStringAdapter.class)
    @XmlSchemaType(name = "NCName")
    protected String identifier;
    @XmlAttribute(name = "identifierref", required = false)
    @XmlJavaTypeAdapter(CollapsedStringAdapter.class)
    @XmlSchemaType(name = "NCName")
    protected String identifierref;
    @XmlAttribute(name = "isvisible", required = false)
    protected boolean isvisible; 

    //I think here is accessors
    List[Items] getItems ...


}

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

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