如何使用JDOM解析以下XML [英] How can i parse the following XML using JDOM

查看:198
本文介绍了如何使用JDOM解析以下XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

现在我要解析使用Java中的jdom在XML上面。它是一种递归和重要的注意事项是问题不能是问题的直接孩子,同样适用于答案

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.

推荐答案

文章

根据之前的相关问题,我想重复并强调别人的建议(如JB Nizet评论)在这个问题上):

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):

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

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.

我知道这可能看起来很苛刻,但它已达到你的程序由StackOverflow用户而不是你自己构建的程度。

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.

话虽如此,这个回答的顶部链接r导致使用JDOM遍历XML的教程。

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

这篇关于如何使用JDOM解析以下XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆