Salut
J'ai essayé d'ercire un programme en java qui me permet de lire un fichier XML et affichier les valeurs souhaité. Mais lorsque j'ai essayé de changer ce programme et mettre les valeurs a récupérées dans un vector puis j'ai essayé d'afficher ces valeurs j'a reçu une resultat incorrcte lorsque j'ai exécuté ce code.
Voici mon code:
package xml;
import java.io.File;
import java.util.List;
import java.util.Vector;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;
/**
* @author root
*
*/
public class TestXML {
/**
* @param args
*/
public org.jdom.Document document;
public Element racine;
public Vector<Object> h;
String nom, cin;
public static void main(String[] args) {
try {
TestXML test = new TestXML();
// On crée une instance de SAXBuilder
SAXBuilder sxb = new SAXBuilder();
// On crée un nouveau document JDOM avec en argument le fichier
// XML
// Le parsing est terminé
test.document = sxb.build(new File("/root/user.xml"));
// On initialise un nouvel élément racine avec l'élément racine du
// document.
test.racine = test.document.getRootElement();
// System.out.println(test.racine);
test.h= new Vector<Object>();
//on recupere l'element results
Element results = (Element)test.racine.getChildren().get(1);
//System.out.println(results);
Element result = (Element)results.getChildren().get(0);
//on a recupere les binding
List<Element> listparam = result.getChildren();
for (Element e : listparam) {
// On affiche le nom de l'element courant
//System.out.println(e);
for(Element el : (List<Element>)e.getChildren())
{
if(el.getName().equals("literal"))
{
test.h.addElement(el.getText());
test.nom=(String) test.h.firstElement();
test.cin=(String) test.h.lastElement();
System.out.println("nom:" + test.nom + "\t cin: "+ test.cin);
}//fin if
}//fin for
}//fin for
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
Voici le resultat que j'obtient:
nom:Habib Youssef cin: Habib Youssef
nom:Habib Youssef cin: 986754
nehlouchaMon fichier XML a la structure suivante:
<?xml vresion "1.0"?>
<sparql>
<head>
<variable name="nom"/>
<variable name="p"/>
<variable name="cin"/>
</head>
<results>
<result>
<binding name="nom">
<uri>http://www.owl-ontologies.com/Ontology1239120737.owl#admin1
</uri>
</binding>
<binding name="p">
<literal datatype="http://www.w3.org/2001/XMLSchema#string">Habib Youssef</literal>
</binding>
<binding name="cin">
<literal datatype="http://www.w3.org/2001/XMLSchema#int">986754</literal>
</binding>
</result>
</results>
</sparql>S'il vous plait aidez moi .
Merci d'avance pour votre aide