在Java中使用XPath解析XML - 使用XPath和NodeList从XML文件中获取数据

6

我有一个xml文件,想要使用Xpath获取一些值。

已经完成了一半的工作,但在文件的最后一部分(States节点)遇到了一些麻烦。

<?xml version="1.0" encoding="UTF-8"?>
<favoris>
    <workflow codewf="wf1000">
        <information>
            <title>wf1</title>
            <desc>description 1</desc>
            <nberState>2</nberState>
            <text>text text text text text text text</text>
        </information>
        <states>
            <state id="1" IDemployee="2">description1</state>
            <state id="2" IDemployee="3">description2</state>
        </states>
    </workflow>

    <workflow codewf="wf2000">
        <information>
            <title>wf2</title>
            <desc>description 2</desc>
            <nberState>3</nberState>
            <text>text text text text text text text</text>
        </information>
        <states>
            <state id="1" IDemployee="3">description1</state>
            <state id="2" IDemployee="2">description2</state>
            <state id="3" IDemployee="4">description2</state>
        </states>
    </workflow>

</favoris>

And here the java code: package myxml;

import java.io.FileReader;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathFactory;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
public class xmlParty {
  public static void main(String[] args) throws Exception {
    XPathFactory factory = XPathFactory.newInstance();
    XPath xPath = factory.newXPath();
    NodeList favoris = (NodeList) xPath.evaluate("/favoris/workflow[@codewf='wf1000']", 
            new InputSource(new FileReader("a.xml")), 
            XPathConstants.NODESET);
    for (int i = 0; i < favoris.getLength(); i++) {
      Element workflow = (Element) favoris.item(i);
      String title = xPath.evaluate("information/title", workflow);
      String desc_w = xPath.evaluate("information/desc", workflow);
      String nberState = xPath.evaluate("information/nberState", workflow);
      String text = xPath.evaluate("information/text", workflow);
      System.out.println(workflow.getAttribute("codewf") +" "+title + " " + desc_w + " " + nberState + " " + text );

      NodeList States = (NodeList)xPath.evaluate("states/state", workflow, XPathConstants.NODESET);
      System.out.println(States.getLength());
      for (int k = 0; k < States.getLength(); k++) {
          String desc_state = xPath.evaluate("states/state", workflow);
          System.out.println(desc_state ); 
      }


   }
  }
}

并且输出将会是:

第一个例子

wf1000 wf1 description 1 2 text text text text text text text
2
description1
description1

Second example

wf2000 wf2 description 2 3 text text text text text text text
3
description1
description1
description1

查看ID为2的状态,文本应为description2而不是description1。 我认为解析器没有移动到第二个子节点,它始终停留在第一个子节点。 那么我该怎么做呢?还有,如何获取状态的属性?


你难道不应该迭代States NodeList吗?! - user357812
我在http://stackoverflow.com/a/21890347/3245218上得到了一个很棒的答案。 - Sawan Kumar
1个回答

3

您需要做类似以下的操作:

for (int k = 0; k < States.getLength(); k++) {
          String desc_state = xPath.evaluate("states/state[position()=" + (k + 1) + "]", workflow);
          String id_employee = xPath.evaluate("states/state[position()=" + (k + 1) + "]/@IDemployee", workflow);
          System.out.println(desc_state + ":" + id_employee); 
}

谢谢您的快速回复,如何处理属性? - Ali Ben Messaoud
@nabeelmukhtar:我正在使用Java中的XPath进行XML签名,但XPath转换不起作用。请参见此链接-http://stackoverflow.com/questions/10698287/xpath-transformation-not-working-in-java - Ashwin

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接