问题
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. in this xml i have nested element with the same name.
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:
@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" ! 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?
回答1:
May be this could help: JAXB endless data structure, recursive binding?
@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 ...
}
来源:https://stackoverflow.com/questions/17115355/parsing-nested-same-name-elements-using-jaxb