IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Format d'échange (XML, JSON...) Java Discussion :

Récupérer les attributs d'un fichier XML en XPATH AVEC DOM [DOM]


Sujet :

Format d'échange (XML, JSON...) Java

  1. #1
    Nouveau membre du Club
    Homme Profil pro
    Architecte de base de données
    Inscrit en
    Juin 2013
    Messages
    53
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Architecte de base de données
    Secteur : Administration - Collectivité locale

    Informations forums :
    Inscription : Juin 2013
    Messages : 53
    Points : 30
    Points
    30
    Par défaut Récupérer les attributs d'un fichier XML en XPATH AVEC DOM
    je cherche à récupérer certains attributs déclarés dans un noeud d'un fichier XML en xpath avec DOM.
    <?xml version="1.0" encoding="ISO-8859-1"?>
    <inventaire>
    <produit code="1" prix="432.00" quantité= "43" />
    <produit code="32" prix="32.00" quantité= "100" />
    <produit code="321" prix="31.00" quantité= "200" />
    </inventaire>
    je cherche a extraire le prix correspondant à l'item ayant le code de produit 321 voila mon programme :

    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
    import org.w3c.dom.*; 
    import javax.xml.*; 
    import javax.xml.parsers.*; 
    import javax.xml.transform.*; 
    import javax.xml.transform.dom.*;  
    import javax.xml.xpath.*; 
    import javax.xml.namespace.*; 
    import java.io.*;
     
    public class cherche{
    	public static void evaluerDOM(Document document, String expression, QName retour){
    		try{
    			//création du XPath 
    			XPathFactory fabrique = XPathFactory.newInstance();
    			XPath xpath = fabrique.newXPath();
     
    			//évaluation de l'expression XPath
    			XPathExpression exp = xpath.compile(expression);
    			Object resultat = exp.evaluate(document,retour);
     
    			System.out.println(resultat);
    		}catch(XPathExpressionException xpee){
    			xpee.printStackTrace();
    		}
    	}
     
    	public static void main(String[] args){
    		try{
    			DocumentBuilderFactory fabrique = DocumentBuilderFactory.newInstance();
     
    			DocumentBuilder constructeur = fabrique.newDocumentBuilder();
     
    			File xml = new File("fichier.xml");
    			Document document = constructeur.parse(xml);
    			evaluerDOM(document,"//produit[@code='321']", XPathConstants.STRING);
    		}catch(Exception e){
    			e.printStackTrace();	
    		}
     
     
    	}	
    }
    mais j'arrive pas a avoir le résultat merci .

  2. #2
    Modérateur
    Avatar de joel.drigo
    Homme Profil pro
    Ingénieur R&D - Développeur Java
    Inscrit en
    Septembre 2009
    Messages
    12 430
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 54
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Ingénieur R&D - Développeur Java
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Septembre 2009
    Messages : 12 430
    Points : 29 131
    Points
    29 131
    Billets dans le blog
    2
    Par défaut
    Salut,

    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
    public class cherche{
    	public static void evaluerDOM(Document document, String expression, QName retour){
    		try{
    			//création du XPath 
    			XPathFactory fabrique = XPathFactory.newInstance();
    			XPath xpath = fabrique.newXPath();
     
    			//évaluation de l'expression XPath
    			XPathExpression exp = xpath.compile(expression);
     
    			Element elem = (Element)exp.evaluate(document,retour);
    			System.out.println(elem.getNodeName());
    			System.out.println("Code : "+elem.getAttribute("code"));
    			System.out.println("Prix : "+elem.getAttribute("prix"));
    			System.out.println("Quantité : "+elem.getAttribute("quantité"));
    			
    		}catch(XPathExpressionException xpee){
    			xpee.printStackTrace();
    		}
    	}
     
    	public static void main(String[] args){
    		try{
    			DocumentBuilderFactory fabrique = DocumentBuilderFactory.newInstance();
     
    			DocumentBuilder constructeur = fabrique.newDocumentBuilder();
     
    			File xml = new File("fichier.xml");
    			Document document = constructeur.parse(xml);
    			evaluerDOM(document,"//produit[@code='321']", XPathConstants.NODE);
    				}catch(Exception e){
    			e.printStackTrace();	
    		}
     
     
    	}	
    }

  3. #3
    Modérateur
    Avatar de joel.drigo
    Homme Profil pro
    Ingénieur R&D - Développeur Java
    Inscrit en
    Septembre 2009
    Messages
    12 430
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 54
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Ingénieur R&D - Développeur Java
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Septembre 2009
    Messages : 12 430
    Points : 29 131
    Points
    29 131
    Billets dans le blog
    2
    Par défaut
    et voilà le code si tu veux n'obtenir que l'attribut prix :

    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
    public static void evaluerDOM(Document document, String expression, QName retour){
    		try{
    			//création du XPath 
    			XPathFactory fabrique = XPathFactory.newInstance();
    			XPath xpath = fabrique.newXPath();
     
    			//évaluation de l'expression XPath
    			XPathExpression exp = xpath.compile(expression);
     
    			String resultat = (String)exp.evaluate(document,retour);
    			System.out.println(resultat);
    			
    		}catch(XPathExpressionException xpee){
    			xpee.printStackTrace();
    		}
    	}
     
    	public static void main(String[] args){
    		try{
    			DocumentBuilderFactory fabrique = DocumentBuilderFactory.newInstance();
     
    			DocumentBuilder constructeur = fabrique.newDocumentBuilder();
     
    			File xml = new File("fichier.xml");
    			Document document = constructeur.parse(xml);
    			evaluerDOM(document,"//produit[@code='321']/@prix", XPathConstants.STRING);
    				}catch(Exception e){
    			e.printStackTrace();	
    		}
     
     
    	}

  4. #4
    Nouveau membre du Club
    Homme Profil pro
    Architecte de base de données
    Inscrit en
    Juin 2013
    Messages
    53
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Architecte de base de données
    Secteur : Administration - Collectivité locale

    Informations forums :
    Inscription : Juin 2013
    Messages : 53
    Points : 30
    Points
    30
    Par défaut
    Merci beaucoup joel.drigo pour votre aide

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. [XML] Récupérer les données d'un fichier XML sur URL avec php
    Par ValooWart dans le forum Bibliothèques et frameworks
    Réponses: 6
    Dernier message: 01/03/2013, 16h40
  2. [XPATH] Récupérer les attributs d'un fichier XML
    Par anizlewan dans le forum Format d'échange (XML, JSON...)
    Réponses: 8
    Dernier message: 15/08/2012, 12h44
  3. affiché les attributs d'un fichier xml dans un treeview
    Par knuj0 dans le forum Windows Presentation Foundation
    Réponses: 5
    Dernier message: 23/09/2010, 08h05
  4. [DOM] Récupérer les attributs d'un fichier XML en PHP
    Par ePsymon dans le forum Bibliothèques et frameworks
    Réponses: 2
    Dernier message: 08/02/2008, 11h51
  5. [](VB) Récupérer les données dans un fichier .xml
    Par Furius dans le forum VBScript
    Réponses: 4
    Dernier message: 02/10/2005, 20h39

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo