Bonjour,
Je cherche à parcourir un document xml et j'ai des balises du genre
<Field Name="toto" type="string">
<![CDATA[titit tutu]]>
</Field>
Et j'essaye de passer par le cast d'un type Node à un type Element, mais j'arrive à chaque à cette erreur.
java.lang.ClassCastException: com.sun.org.apache.xerces.internal.dom.DeferredTex
tImpl cannot be cast to org.w3c.dom.Element
Voici l'exemple du code
Voici un extrait du fichier xml que je parcours pour importer les données se trouvant dans les balises field.
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 ... if (document != null) { /*Liste items */ NodeList items = document.getElementsByTagName("ITEMS"); if (items != null && items.getLength() > 0) { /*parcours de la liste des items*/ for (int i = 0; i < items.getLength(); i++) { Node itemList = items.item(i); System.out.println("1 "+itemList.getNodeName()); NodeList item = itemList.getChildNodes(); /*parcours des contenus d'un item*/ for (int j = 0; j < item.getLength(); j++) { /*on récupère une liste de fields*/ Node fields = item.item(j); System.out.println("2 "+fields.getNodeName()); System.out.println("type = "+fields.getNodeType()); /*Element link = (Element) fields; System.out.println(link.getAttribute("ID")); System.out.println(link.getAttribute("Title")); */ NodeList fieldList = fields.getChildNodes(); for (int k = 0; k < fieldList.getLength(); k++) { Node field = fieldList.item(k); System.out.println("3 "+field.getNodeName()); NodeList children = field.getChildNodes(); /*parcours de field*/ for (int l = 0; l < children.getLength(); l++) { Node child = children.item(l); Element link = (Element) child; System.out.println("4 "+child.getNodeName()); System.out.println(child.getTextContent()); System.out.println("type = "+child.getNodeType()); } } } } } } ...
Le problème est que je n'arrive pas à récupérer la valeur du Name dans le
<Field Name="toto" type="string">
<![CDATA[titit tutu]]>
</Field>
Merci d'avance pour votre aide
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 <?xml version="1.0" encoding="utf-8"?> <LISTS> <LIST Name="offres_emploi" Url="http://universalis..."> <ITEMS> <ITEM Title="technicien station" ID="{...}"> <FIELDS> <FIELD Name="Title" type="string"><![CDATA[technicien station]]></FIELD> <FIELD Name="piecejointe" type="document"><![CDATA[{identifiant_doc_1}]]></FIELD> </FIELDS> </ITEM> </ITEMS> </LIST> <LIST Name="library" Url="http://....."> <DOCUMENTS> <DOCUMENT Name="offre_technicien.pdf" ID="{identifiant_doc_1}"> <FIELDS> <FIELD Name="name" type="string"><![CDATA[description de l'offre]]></FIELD> <FIELD Name="description" type="string"><![CDATA[]]></FIELD> <FIELD Name="type" type="string"><![CDATA[Document]]></FIELD> </FIELDS> </DOCUMENT> </DOCUMENTS> </LIST> </LISTS>
Partager