簡體   English   中英

使用Java中的XPath解析XML - 使用Java中的Xpath和NodeList從XML文件中獲取數據

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

我有這個xml文件,我想用Xpath獲取一些值。

工作的一半已經完成但我在文件的最后部分遇到了一些麻煩(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>

這里是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 ); 
      }


   }
  }
}

輸出將是:

第一個例子

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

第二個例子

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

查看ID為2的狀態,文本為description2而不是description1 我認為解析器不會移動到第二個孩子,它總是仍然在最小的孩子。 那么我該怎么做以及如何做才能獲得狀態的屬性????????

你將不得不做以下事情:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM