Bonjour a tous,
sur un projet je cherche a récupérer des valeurs dans des tags et en faire un fichier texte.
Je me suis donc inspiré d'un ancien code mais je n'arrive avec celui-ci qu'a récupérer tout ou rien
voici mon xml:
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151 <?xml version="1.0" encoding="UTF-8"?> <AIXM-Snapshot created="2019-01-08T11:04:34.997+01:00" origin="Sia-France" version="4.5" effective="2019-02-28T00:00:00.000+01:00"> <Org> <OrgUid mid="1520776"> <txtName>POLYNESIE FRANCAISE</txtName> </OrgUid> <codeId>NT</codeId> <codeType>O</codeType> </Org> <Abd> <AbdUid mid="1569504"> <AseUid mid="1561979"> <codeType>D-OTHER</codeType> <codeId>LF9013</codeId> </AseUid> </AbdUid> <Avx> <codeType>GRC</codeType> <geoLat>470440.00N</geoLat> <geoLong>0020137.00E</geoLong> <codeDatum>WGE</codeDatum> </Avx> </Abd> <Abd> <AbdUid mid="1569508"> <AseUid mid="1561977"> <codeType>D-OTHER</codeType> <codeId>LF6366</codeId> </AseUid> </AbdUid> <Avx> <codeType>GRC</codeType> <geoLat>474538.00N</geoLat> <geoLong>0032624.00W</geoLong> <codeDatum>WGE</codeDatum> </Avx> </Abd> <Abd> <AbdUid mid="1568676"> <AseUid mid="1561155"> <codeType>P</codeType> <codeId>SO3</codeId> </AseUid> </AbdUid> <Avx> <codeType>GRC</codeType> <geoLat>050630.00N</geoLat> <geoLong>0523140.00W</geoLong> <codeDatum>WGE</codeDatum> </Avx> <Avx> <codeType>GRC</codeType> <geoLat>045930.00N</geoLat> <geoLong>0523614.00W</geoLong> <codeDatum>WGE</codeDatum> </Avx> <Avx> <codeType>CWA</codeType> <geoLat>051330.00N</geoLat> <geoLong>0530100.00W</geoLong> <codeDatum>WGE</codeDatum> <geoLatArc>051815.00N</geoLatArc> <geoLongArc>0525005.00W</geoLongArc> <valRadiusArc>11.5</valRadiusArc> <uomRadiusArc>NM</uomRadiusArc> </Avx> <Avx> <codeType>GRC</codeType> <geoLat>053000.00N</geoLat> <geoLong>0525200.00W</geoLong> <codeDatum>WGE</codeDatum> </Avx> <Avx> <codeType>GRC</codeType> <geoLat>051635.00N</geoLat> <geoLong>0523520.00W</geoLong> <codeDatum>WGE</codeDatum> </Avx> </Abd> <Abd> <AbdUid mid="1569516"> <AseUid mid="1561903"> <codeType>D-OTHER</codeType> <codeId>LF984</codeId> </AseUid> </AbdUid> <Avx> <codeType>GRC</codeType> <geoLat>433629.00N</geoLat> <geoLong>0064206.00E</geoLong> <codeDatum>WGE</codeDatum> </Avx> </Abd> <Abd> <AbdUid mid="1569568"> <AseUid mid="1562817"> <codeType>R</codeType> <codeId>LFR10</codeId> </AseUid> </AbdUid> <Avx> <codeType>GRC</codeType> <geoLat>493555.00N</geoLat> <geoLong>0011302.00W</geoLong> <codeDatum>WGE</codeDatum> </Avx> <Avx> <codeType>GRC</codeType> <geoLat>492939.00N</geoLat> <geoLong>0002154.00E</geoLong> <codeDatum>WGE</codeDatum> </Avx> <Avx> <codeType>GRC</codeType> <geoLat>492307.00N</geoLat> <geoLong>0002732.00E</geoLong> <codeDatum>WGE</codeDatum> </Avx> <Avx> <codeType>GRC</codeType> <geoLat>491130.00N</geoLat> <geoLong>0002100.00E</geoLong> <codeDatum>WGE</codeDatum> </Avx> <Avx> <codeType>GRC</codeType> <geoLat>491518.00N</geoLat> <geoLong>0002635.00W</geoLong> <codeDatum>WGE</codeDatum> </Avx> <Avx> <codeType>GRC</codeType> <geoLat>491701.00N</geoLat> <geoLong>0011756.00W</geoLong> <codeDatum>WGE</codeDatum> </Avx> <Avx> <codeType>GRC</codeType> <geoLat>492403.00N</geoLat> <geoLong>0012832.00W</geoLong> <codeDatum>WGE</codeDatum> </Avx> <Avx> <codeType>GRC</codeType> <geoLat>492937.00N</geoLat> <geoLong>0012324.00W</geoLong> <codeDatum>WGE</codeDatum> </Avx> </Abd> </AIXM-Snapshot>
De ce XML je desire ne récupérer que les valeurs dans les balises codeId et les balises geoLAT geoLong l'ensemble dans chaque balise Abd et enfant Avx
pour cela j'ai ce code que j'ai essayé d'adapter a mon besoin (voir les commentaires) mais je n'y arrive pas
Or avec celui-ci je n'obtiens que ce qu'il y a dans les balises Avx (par exemple) en totalité.
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97 package extraction_xml; import java.io.*; import javax.xml.xpath.*; import org.w3c.dom.Document; import org.xml.sax.SAXException; import javax.xml.namespace.*; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import org.w3c.dom.Node; import org.w3c.dom.NodeList; public class Extraction1 { static File fichier; // Constructeur public Extraction1 (String url){ fichier = new File(url); } // Méthodes /** * créer une instance de XPath et lui faire évaluer une XPathExpression * @param file * @param expression * @param expression2 * @param expression3 * @param retour * @return */ public String evaluerDOM(File file, String expression, QName retour){ String texteCopie=null; try{ DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder constructeur = docBuilderFactory.newDocumentBuilder(); Document document = constructeur.parse(file); //création du XPath XPathFactory fabrique = XPathFactory.newInstance(); XPath xpath = fabrique.newXPath(); //évaluation de l'expression XPath XPathExpression exp = xpath.compile(expression); NodeList resultat = (NodeList) exp.evaluate(document,XPathConstants.NODESET); // récupère la liste de Node correspondant au xpath /*XPathExpression exp2 = xpath.compile(expression2); NodeList resultat2 = (NodeList) exp2.evaluate(document,XPathConstants.NODESET); // récupère la liste de Node correspondant au xpath /*XPathExpression exp3 = xpath.compile(expression3); NodeList resultat3 = (NodeList) exp3.evaluate(document,XPathConstants.NODESET); // récupère la liste de Node correspondant au xpath */ StringBuilder sb=new StringBuilder(); // pour faire la concaténation for(int i=0; i<resultat.getLength(); i++) { // parcours if ( i!=0 ) { // pour avoir des séparations entre chaque node, sauf pour le premier sb.append("--------------------------\n");//ajout d'une ligne de séparation par exemple //sb.append("\n"); // ajoute une ligne vide } //sb.append("name:");//ajout de name devant codeId //sb.append("\n"); // ajoute une ligne vide sb.append("Zone ").append(i+1).append("");//ajout d'un titre par exemple Node Avx = resultat.item(i); // on récupère le node d'index i sb.append(Avx.getTextContent()); // concaténation du texte du node }texteCopie=sb.toString(); // }catch(XPathExpressionException | ParserConfigurationException | SAXException | IOException xpee){ } return texteCopie; } /** * @param args * @throws java.io.IOException */ public static void main(String[ ] args) throws IOException { Extraction1 test = new Extraction1("/Users/moi/Desktop/AIXM_test_macro.xml"); String resultat = test.evaluerDOM(fichier, "//Avx",XPathConstants.STRING); //String resultat2 = test.evaluerDOM(fichier, "//Abd/Avx/geoLon",XPathConstants.STRING); File AIP = new File("/Users/moi/Desktop/AIP_test.txt") ; try (PrintWriter out = new PrintWriter(new FileWriter(AIP))) { out.write(resultat) ; //écris resultat dans le fichier //out.println(); //fais un retour à la ligne dans le fichier out.close() ; //Ferme le flux du fichier, sauvegardant ainsi les données. } } }
en fait je souhaite récupérer le nom plus les coordonnées (sachant qu'il peut y en avoir plusieurs) pour un <Abd>...</Abd>
Pourriez-vous m'aider s'il vous plait?
Merci par avance pour vos réponse.
Amicalement
Partager