简单的Xml列表解析问题
我有这个 xml:
<root>
<fruitlist>
<apple>4</apple>
<apple>5</apple>
<orange>2</orange>
<orange>6</orange>
</fruitlist>
</root>
我正在编写一个解析器类,尽管我不知道如何处理多个节点类型。我可以轻松地解析一个仅包含一种节点类型(例如,仅包含苹果,而不是橙子)的列表,
@ElementList(name = "fruitlist")
private List<Apple> exercises;
并且它还需要解析多个节点类型,因此解析不起作用的非 Apple 节点。 我还尝试为橙子制作另一个列表,但它不起作用,我不能多次使用水果列表名称。
@ElementList(name = "fruitlist", entry = "orange")
private List<Orange> exercises;
理想的情况是两种节点类型都有两个单独的列表。
胡比
编辑: 经过更多搜索&摆弄这个问题是重复的:
I have this xml:
<root>
<fruitlist>
<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:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
试试这个,它会起作用的。
Try this, it will work.
解决此解析问题的理想数据结构可能是 HashTable 以元素的名称作为键(字符串)及其可能的子元素列表作为内容(List
无论如何,如果您想解析 XML 文件,您可以在 Java 中使用许多可能的框架。
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
Anyway if you wanna parse an XML file there are lots of possible framworks you can use in Java.