2011-04-15 6 views
5

J'ai ce fichier xml et je veux obtenir des valeurs avec Xpath.Analyser XML avec XPath en Java - Obtenir des données à partir d'un fichier XML avec Xpath et NodeList en Java

La moitié du travail est fait, mais je reçois quelques problèmes dans la dernière partie du fichier (États 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> 

Et voici le code 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); 
     } 


    } 
    } 
} 

et la sortie sera:

Premier exemple

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

Deuxième exemple

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

En regardant l'état avec ID 2, le texte est description2 pas description1. Je pense que l'analyseur ne passe pas au deuxième enfant et toujours au premier enfant. Alors, comment puis-je faire et aussi comment faire pour obtenir l'attribut d'état ????????

+0

Si vous n'itérer sur les États NodeList ?! –

+0

J'ai eu une réponse briallint de cette question sur http://stackoverflow.com/a/21890347/3245218 –

Répondre

2

Vous devez faire quelque chose comme:

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

Merci pour la réponse rapide et comment faire pour les attributs ?? – alibenmessaoud

+0

J'ai modifié la réponse. – nabeelmukhtar

+0

@nabeelmukhtar: J'utilise xpath dans java pour les signatures xml mais la transformation xpath ne fonctionne pas. S'il vous plaît voir ce lien - http://stackoverflow.com/questions/10698287/xpath-transformation-not-working-in-java – Ashwin

Questions connexes