Bonjour,
Je rencontre un problème d'affichage lorsque j'utilise la fonction getNodeName() quand j'affiche les infos sur des noeuds de mon fichier xml.
Le souci est que cette fonction me retourne non seulement le nom, mais aussi le type du noeud sous cette forme:
Y a-t-il un moyen de supprimer l'affichage de '#text'? Je vous envoie ma fonction et le xml. Merci d'avance pour any help:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9 nom: Flactif #text: prenom: Olivier #text: contrat: Tous risques #text:
Contenu de 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 public static void retournerClientParNom(String nom) { NodeList noeuds = null; XPath xpath = XPathFactory.newInstance().newXPath(); InputSource inputSource = new InputSource("./xml/bd1.xml"); String expression = "/bd//client[nom ='" + nom + "']"; try { noeuds = (NodeList)xpath.evaluate(expression, inputSource, XPathConstants.NODESET); if(noeuds != null) { Node node = noeuds.item(0); NodeList clientInfo = node.getChildNodes(); for (int i = 0; i < clientInfo.getLength(); i++) { Node child = clientInfo.item(i); System.out.println(child.getNodeName()+": "+child.getTextContent()); } } } catch(XPathExpressionException e) { e.printStackTrace(); } }
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 <?xml version="1.0" encoding="iso-8859-1"?> <bd xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="./bd1.xsd"> <client> <nom>Flactif</nom> <prenom>Olivier</prenom> <contrat id="2">Tous risques</contrat> <dateDebut>2005-09-12</dateDebut> <dateFin>2006-09-11</dateFin> <dateNaissance>1972-02-04</dateNaissance> <statut>fonctionnaire</statut> <garantie>protection juridique</garantie> <garantie>frais de santé</garantie> <garantie>assistance accident</garantie> <garantie>logement</garantie> <garantie>appareils électriques</garantie> <garantie>voiture</garantie> <incident date="2006-01-03" objet="appareil electrique">vol</incident> <compensation>145.0</compensation> </client>
Partager