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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5673708/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 12:13:02  来源:igfitidea点击:

Parsing XML with XPath in Java - Get data from XML file with Xpath and NodeList in Java

javaxmlxpathnodelist

提问by Ali Ben Messaoud

I have this xml file and I want to get some values with Xpath.

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

The half of job is done but I get some trouble in the last part of file(States Node)

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

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

这里是java代码: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 ); 
      }


   }
  }
}

and the output will be :

输出将是:

First example

第一个例子

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

Looking at the state with ID 2, the text is description2not description1. I think the parser don't move to the second child and it always still at the firsst child. So how can I do and also how to do too to get the attribute of state????????

查看 ID 为 2 的状态,文本description2不是description1。我认为解析器不会移动到第二个孩子,它总是在第一个孩子。那么我该怎么做以及怎么做才能获得状态的属性??????????

采纳答案by nabeelmukhtar

You will have to do something like:

您将不得不执行以下操作:

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); 
}