Bonjour,
j'ai un souci avec mon code Java qui n'arrive pas à lire mon fichier xml comme je veux. J'ai un problème avec la lecture au niveau des noeuds, le code n'affiche pas les informations des noeuds imbriqués.
J'ai passé par plusieurs moyens mais impossible. Voici mon code :
Fichier xml
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57 package LireXML; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.DocumentBuilder; import org.w3c.dom.Document; import org.w3c.dom.NodeList; import org.w3c.dom.Node; import org.w3c.dom.Element; import java.io.File; public class ReadFileConfigXML2 { public static void main(String argv[]) { try { File File = new File("fichier.xml"); DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); Document doc = dBuilder.parse(File); System.out.println("Root element :" + doc.getDocumentElement().getNodeName()); NodeList nList = doc.getElementsByTagName("fichier"); System.out.println("----------------------------"); for (int temp = 0; temp < nList.getLength(); temp++) { Node nNode = nList.item(temp); System.out.println("\nCurrent Element :" + nNode.getNodeName()); if (nNode.getNodeType() == Node.ELEMENT_NODE) { for(int k=0;k<nList.getLength(); k++) { Element eElement = (Element) nNode; System.out.println("machine type : " + eElement.getAttribute("type")); System.out.println("image type : " + eElement.getAttribute("type")+" path: "+eElement.getAttribute("path")); System.out.println("image type : " + eElement.getAttribute("type")+" path: "+eElement.getAttribute("path")); System.out.println("image type : " + eElement.getAttribute("type")+" path: "+eElement.getAttribute("path")); System.out.println("image type : " + eElement.getAttribute("type")+" path: "+eElement.getAttribute("path")); System.out.println("sortie type : " + eElement.getAttribute("type")); System.out.println("temp-fabrication : " + eElement.getElementsByTagName("temp-fabrication").item(temp).getTextContent()); } } } } catch (Exception e) { e.printStackTrace(); } } }
Je n'arrive pas du tout à lire les informations qui se trouvent dans les balises image.
Code XML : 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
31
32
33
34
35
36
37
38
39 <?xml version="1.0" encoding="UTF-8"?> <fichier> <donnees> <fabricant type="machineA"> <symboles> <image type="0" path="src/ressources/img1.png"/> <image type="1/3" path="src/ressources/img2.png"/> <image type="1/2-tiers" path="src/ressources/img3.png"/> <image type="2/2" path="src/ressources/img4.png"/> </symbole> <fabrique type = "alumette"/> <temp-fabrication>5</temp-fabrication> </fabricant> <machine type="machineC"> <symbole> <image type="0" path="src/ressources/img1.png"/> <image type="1/3" path="src/ressources/img2.png"/> <image type="1/2-tiers" path="src/ressources/img3.png"/> <image type="2/2" path="src/ressources/img4.png"/> </symble> <entree type="metal" quantite="2"/> <fabrique type="carton"/> <temp-fabrication>15</temp-fabrication> </machine> </donnees> <test> <machine type="machineA" id="21" x="23" y="25"/> <machine type="machineC" id="21" x="230" y="320"/> <arret> <chemin de="88" vers="52" /> <chemin de="55" vers="37" /> </arret> </test> </fichier>
Merci d'avance.
Partager