我如何使用 JDOM 解析以下 XML

发布于 2024-12-02 05:12:35 字数 1565 浏览 5 评论 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。需要注意的递归且重要的一点是,问题不能是问题的直接子级,并且同样适用于答案

I have an XML document as follows:

<?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>

Now I want to parse the above XML using jdom in Java. It kind of recursive and important thing to note is a Question can't be a direct child of Question and same applies for Answer.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

橘味果▽酱 2024-12-09 05:12:35

文章

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

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

我知道这可能看起来很严酷,但实际上你的程序是由 StackOverflow 用户而不是你自己构建的。

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

Article

In the light of previous related questions, I'd like to repeat and stress the advice of others (like JB Nizet commented on this question):

Learn Java, learn XML, pick the tools and API's you need for your project and learn to use those too. If at one point you get into trouble, everybody here will be happy to help you out debugging your code.

I'm aware that this may seem harsh but it gets to the point where your program gets built by StackOverflow users and not yourself.

That being said, the link at the top of this answer leads to a tutorial on using JDOM to traverse your XML.

你的背包 2024-12-09 05:12:35

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

Use Element.getChildren(String) to get all of the question tags and loop through that List - calling getChildren(String) to get all of the answers, or getChild(String) if there can be only one child element.

何止钟意 2024-12-09 05:12:35

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

first that you need use is XSD to validate the XML.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文