使用XPath获取子元素

时间:2010-12-16 02:58:59

标签: java xpath

我正在尝试使用DocumentBuilder和XPath来解析XML文档,其结构如下:

<questionnaire>
  <item>
    <question>How have you been?</question>
    <response>Great</response>
    <response>Good</response>
    <response>So-so</response>
    <response>Bad</response>
    <response>Rather not answer</response>
  </item>
</questionnaire>

要访问问题,我已完成此操作(可行):

expression = "/questionnaire/item[" + i + "]/question";
setQuestion(xmlReader.read(expression, XPathConstants.STRING).toString());

现在我需要一些方法来根据响应项创建一个字符串列表。答复的数量是可变的,因此一个问题可以有任意数量的答复。有谁知道怎么做?

由于

1 个答案:

答案 0 :(得分:0)

这样的事情不会这样吗?您必须注意,在这种情况下,xmlReader.read可能会返回一个集合。

expression = "/questionnaire/item[" + i + "]/response";
setResponse(xmlReader.read(expression, XPathConstants.STRING));
相关问题