开发者

Simple Xml list parsing problem

开发者 https://www.devze.com 2023-03-25 08:40 出处:网络
I have this xml: <root> <fruit开发者_C百科list> <apple>4</apple> <apple>5</apple>

I have this xml:

<root>
<fruit开发者_C百科list>
    <apple>4</apple>
    <apple>5</apple>
    <orange>2</orange>
    <orange>6</orange>
</fruitlist>
</root>

I'm writing a parser class, although I can't figure out how to deal with multiple node types. I can easily parse a list that contains only one node type (eg. just apples, not oranges)

@ElementList(name = "fruitlist")
private List<Apple> exercises;

with more than one node type it also wants so parse non Apple nodes which doesn't work. I also tried to make another list for oranges, but it doen't work, I can't use fruitlist name more than once.

@ElementList(name = "fruitlist", entry = "orange")
private List<Orange> exercises;

The ideal would be two seperate list for both node types.

Hubi

EDIT: After more searching & fiddling this question is a duplicate:

Inheritance with Simple XML Framework


Try this, it will work.

@ElementListUnion({
   @ElementList(entry="apple", type=Apple.class),
   @ElementList(entry="orange", type=Orange.class)
})
private List<Fruit> fruitlist;


The ideal data structure to solve this parsing problem would probably be an HashTable with the name of the element as key (String) and the list of his possible child as content (List

Hashtable<String, Integer> numbers = new Hashtable<String, List<String>>();

Anyway if you wanna parse an XML file there are lots of possible framworks you can use in Java.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号