2011-04-15 75 views
5

我有這個xml文件,我想用Xpath獲取一些值。在Java中使用XPath解析XML - 使用Xpath和Java中的NodeList從XML文件中獲取數據

工作的一半是做,但我得到的文件的最後一部分有些麻煩(國節點)

<?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代碼: 包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,則文本是description2description1。 我認爲解析器不會移動到第二個孩子,它總是仍然在第一個孩子。 那麼我該怎麼做,也該怎麼做才能得到狀態的屬性????????

+0

你不應該遍歷美國節點列表? – 2011-04-15 14:46:55

+0

我在http://stackoverflow.com/a/21890347/3245218 – 2014-02-19 19:34:45

回答

2

你將不得不做這樣的事情:

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

得到了這個問題的briallint答案感謝您的快速回復以及如何處理屬性? – alibenmessaoud 2011-04-15 07:45:59

+0

我編輯了答案。 – nabeelmukhtar 2011-04-15 07:47:31

+0

@nabeelmukhtar:我在java中使用xpath作爲xml簽名,但xpath轉換不起作用。請參閱此鏈接 - http://stackoverflow.com/questions/10698287/xpath-transformation-not-working-in-java – Ashwin 2012-05-23 08:00:33