Bonjour,
Je galère depuis ce matin pour parser mon fichier xml que voici :
En gros, j'ai des personnes qui possèdent des documents.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30 <?xml version="1.0" encoding="utf-8"?> <root lang="fr"> <Personne> <Document type="A"> <nom>nom</nom> <Prenom>prenom</Prenom> </Document> <Document type="B"> <nom>nom</nom> <Prenom>prenom</Prenom> </Document> <Info>info</Info> </Personne> <Personne> <Document type="A"> <nom>nom</nom> <Prenom>prenom</Prenom> </Document> <Document type="C"> <nom>nom</nom> <Prenom>prenom</Prenom> </Document> <Info>info</Info> </Personne> </root>
Je veux pour chaque personne extraire ses documents ainsi que les valeurs de chaque document.
Donc il me faut une boucle sur les personnes et pour chaque personne faire une boucle sur ses documents.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); Document doc = db.parse(fichier); Element root = doc.getDocumentElement(); NodeList nodeList = root.getElementsByTagName("Personne"); NodeList nl = root.getElementsByTagName("Document"); if(nl != null && nl.getLength() > 0) { for(int i = 0 ; i < nl.getLength();i++) { Element el = (Element)nl.item(i); String name1 = getTextValue(el,"nom"); String name2 = getTextValue(el,"prenom"); } }En gros, je n'arrive pas à faire le lien entre personne et documents. Sachant qu'une personne à d'autres infos en plus que ses documents.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11 private String getTextValue(Element ele, String tagName) { String textVal = null; NodeList nl = ele.getElementsByTagName(tagName); if(nl != null && nl.getLength() > 0) { Element el = (Element)nl.item(0); textVal = el.getFirstChild().getNodeValue(); } return textVal; }
Merci pour votre aide.
Partager