0

我有一个如下的 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

4

3 回答 3

2

文章

鉴于之前的相关问题,我想重复并强调其他人的建议(如 JB Nizet 对此问题的评论):

学习 Java,学习 XML,选择项目所需的工具和 API,并学习使用它们。如果您遇到麻烦,这里的每个人都会很乐意帮助您调试代码。

我知道这可能看起来很苛刻,但它会导致您的程序由 StackOverflow 用户而不是您自己构建。

话虽如此,此答案顶部的链接指向有关使用 JDOM 遍历 XML 的教程。

于 2011-08-29T15:55:37.350 回答
1

用于Element.getChildren(String)获取所有问题标签并遍历该列表 - 调用getChildren(String)以获取所有答案,或者getChild(String)如果只能有一个子元素。

于 2011-08-29T15:58:11.253 回答
0

首先需要使用 XSD 来验证 XML。

于 2011-08-29T15:14:17.107 回答