使用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.
我有一个巨大的xml用于e-学习平台。在这个xml中,我有一个同名的嵌套元素。
I am having a huge xml for an e-learning platform. in this xml i have nested element with the same name.
如:
<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>
我们可以看到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.
这是我的Item java类:
Here is my Item java class:
@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;
例如,每当我调用任何子项目的标题时,我总是主要的父标题欢迎!这意味着我无法进入递归。虽然经过长时间的调试后我的方法完全正确...每当我调用getItems()时,我得到[]。
任何帮助?
For example, whenever i call a title of any of the child items i always the main parent title "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 []. any help?
推荐答案
可能会有所帮助: 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 ...
}
这篇关于使用jaxb解析嵌套的相同名称元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!