我有一个 XML 文档,如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<decision>
<question id="0">
<questionText>What type is your OS?</questionText>
<answer id="0">
<answerText>windows</answerText>
</answer>
<answer id="1">
<answerText>linux</answerText>
</answer>
<answer id="2">
<answerText>mac</answerText>
</answer>
</question>
<question id="1">
<questionText>What are you looking for?</questionText>
<answer id="0">
<answerText>table</answerText>
<question id="0">
<questionText>Which color table you want?</questionText>
<answer id="0">
<answerText>green</answerText>
</answer>
<answer id="1">
<answerText>black</answerText>
</answer>
<answer id="2">
<answerText>pink</answerText>
</answer>
</question>
</answer>
<answer id="1">
<answerText>chair</answerText>
</answer>
<answer id="2">
<answerText>bed</answerText>
</answer>
<answer id="3">
<answerText>cloth</answerText>
</answer>
</question>
现在我想在Java中使用jdom解析上面的XML。这是一种递归,需要注意的重要一点是Question不能是以下对象的直接子对象Question同样适用于Answer.
Article http://www.cafeconleche.org/books/xmljava/chapters/ch14s08.html
鉴于之前的相关问题,我想重复并强调其他人的建议(就像 JB Nizet 评论的那样)这个问题 https://stackoverflow.com/q/7203211/701884):
学习 Java、学习 XML、选择项目所需的工具和 API,并学习使用它们。如果您有一天遇到麻烦,这里的每个人都会很乐意帮助您调试代码。
我知道这可能看起来很严酷,但实际上你的程序是由 StackOverflow 用户而不是你自己构建的。
话虽如此,此答案顶部的链接指向有关使用 JDOM 遍历 XML 的教程。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)