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 :

Parser avec JDOM [JDOM]


Sujet :

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

  1. #1
    Expert confirmé
    Avatar de GLDavid
    Homme Profil pro
    Service Delivery Manager
    Inscrit en
    Janvier 2003
    Messages
    2 867
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 47
    Localisation : France, Seine Saint Denis (Île de France)

    Informations professionnelles :
    Activité : Service Delivery Manager
    Secteur : Industrie Pharmaceutique

    Informations forums :
    Inscription : Janvier 2003
    Messages : 2 867
    Points : 4 873
    Points
    4 873
    Par défaut Parser avec JDOM
    Bonjour !

    Longtemps que je n'ai plus fréquenté le forum Java !
    Bon, blague à part, je me met à parser des fichiers XML avec JDOM. Voici un exemple de fichier XML que je veux parser :
    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
     
    <?xml version="1.0" encoding="UTF-8"?>
     
    <!--
        Document   : Essai.xml.xml
        Created on : August 11, 2004, 12:15 PM
        Author     : David
        Description:
            Purpose of the document follows.
    -->
     
    <interactorList>
                    <proteinInteractor id="PARP3">
                    <names>
                    <shortLabel>PARP3</shortLabel>
                    </names>
                    <xref>
                    <primaryRef db="SP" id="Q9Y6F1">
                    </primaryRef>
                    <secondaryRef id="9473509" db="pubmed"></secondaryRef>
                    </xref>
                    <organism ncbiTaxId="9606">
                    <names><shortLabel>Human</shortLabel><fullName>Homo Sapiens</fullName></names></organism>
                    <sequence>&gt;madprdkalq dyrkkllehk eidgrlkelr
    			eqlkeltkqy eksendlkal qsvgqivgev
    			lkqlteekfi vkatngpryv vgcrrqldks
    			klkpgtrval dmttltimry lprevdplvy
    			nmshedpgnv syseigglse qirelrevie
    			lpltnpelfq rvgiippkgc llygppgtgk
    			tllaravasq ldcnflkvvs ssivdkyige
    			sarliremfn yardhqpcii fmdeidaigg
    			rrfsegtsad reiqrtlmel lnqmdgfdtl
    			hrvkmimatn rpdtldpall rpgrldrkih
    			idlpneqarl dilkihagpi tkhgeidyea
    			ivklsdgfng adlrnvctea gmfairadhd
    			fvvqedfmka vrkvadskkl eskldykpv</sequence></proteinInteractor>
                            </interactorList>
    Je veux récupérer la valeur du tag "id" dans la balise proteinInteractor.
    Voici mon code :
    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
     
    import java.io.*;
    import org.jdom.*;
    import org.jdom.input.*;
    import org.jdom.filter.*;
    import java.util.List;
    import java.util.Iterator;
    /**
     *
     * @author  David
     */
    public class Makanko2 {
     
        public static void listChildren(Element current, int depth) {
     
            List children = current.getChildren();
            Iterator iterator = children.iterator();
            while (iterator.hasNext()) {
                Element child = (Element)iterator.next();
                Element child2 = child.getChild("proteinInteractor");
                System.out.println(child2.getAttribute("id").getValue());
            }
     
        }
     
        public static void main(String[] args) {
     
            SAXBuilder builder = new SAXBuilder();
     
            try {
                Document doc = builder.build("Essai.xml");
                Element root = doc.getRootElement();
                listChildren(root, 0);
            }
            // indicates a well-formedness error
            catch (JDOMException e) {
                System.out.println("Essai.xml is not well-formed.");
                System.out.println(e.getMessage());
            }
            catch (IOException e) {
                System.out.println(e);
            }
     
        }
     
    }
    Or, malheureusement, je pars en pointeur null au moment du System.out.println dans la méthode listChildren.
    Où est mon erreur ?

    Merci de vos conseils !

    @ ++

  2. #2
    Membre émérite
    Avatar de Ioan
    Profil pro
    Inscrit en
    Juin 2003
    Messages
    737
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juin 2003
    Messages : 737
    Points : 2 999
    Points
    2 999
    Par défaut
    Bonjour,
    Tu vas un cran trop loin. Ton root est <interactorList>. Tu fais un iterator.next donc tu te retrouves sur <proteinInteractor id="PARP3">. Puis tu lui demande un enfant <proteinInteractor> qui n'existe pas.
    Essaye :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
        public static void listChildren(Element current, int depth) { 
     
            List children = current.getChildren(); 
            Iterator iterator = children.iterator(); 
            while (iterator.hasNext()) { 
                Element child = (Element)iterator.next(); 
                System.out.println(child);
    			System.out.println(child.getAttribute("id").getValue());
            } 
     
        }
    Bonne chance.
    @+

  3. #3
    Expert confirmé
    Avatar de GLDavid
    Homme Profil pro
    Service Delivery Manager
    Inscrit en
    Janvier 2003
    Messages
    2 867
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 47
    Localisation : France, Seine Saint Denis (Île de France)

    Informations professionnelles :
    Activité : Service Delivery Manager
    Secteur : Industrie Pharmaceutique

    Informations forums :
    Inscription : Janvier 2003
    Messages : 2 867
    Points : 4 873
    Points
    4 873
    Par défaut
    Cool !! :o Merci Vedaer !!
    J'avais aussi trouvé ce code à défaut :
    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
     
    import java.io.*;
    import org.jdom.*;
    import org.jdom.input.*;
    import org.jdom.filter.*;
    import java.util.List;
    import java.util.Iterator;
    /**
     *
     * @author  David
     */
    public class Makanko2 {
     
        public static void listChildren(Document d) {
     
            Iterator i = d.getRootElement().getChildren().iterator();
            while (i.hasNext()) {
                String url;
     
                org.jdom.Element e = (Element)i.next();  
     
                url = e.getAttributeValue("id");
                System.out.println(url);
     
                String phone = e.getChildText("sequence");
                if (phone==null) System.out.println("Ke dalle");
                else System.out.println(""+phone);
     
                System.out.println();
            }
     
        }
     
        public static void main(String[] args) {
     
            SAXBuilder builder = new SAXBuilder();
     
            try {
                Document doc = builder.build("Essai.xml");
                listChildren(doc);
            }
            catch (JDOMException e) {
                System.out.println("Essai.xml is not well-formed.");
                System.out.println(e.getMessage());
            }
            catch (IOException e) {
                System.out.println(e);
            }
     
        }
     
    }
    Et c'est beau dans les 2 cas !
    Merci encore !

    @ ++

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

Discussions similaires

  1. [JDOM] Parser un fichier XML avec JDOM
    Par onsanaisou dans le forum Format d'échange (XML, JSON...)
    Réponses: 1
    Dernier message: 11/06/2014, 10h46
  2. [JDOM] Parser du xml sous forme de string avec JDOM
    Par Lord Yu dans le forum Format d'échange (XML, JSON...)
    Réponses: 2
    Dernier message: 02/02/2010, 12h47
  3. GWT : Parser du XML sur le client avec JDOM ?
    Par sz1708 dans le forum GWT et Vaadin
    Réponses: 4
    Dernier message: 22/10/2008, 10h10
  4. [JDOM] parser fichier xml avec JDOM
    Par sal.gass dans le forum Format d'échange (XML, JSON...)
    Réponses: 5
    Dernier message: 17/06/2008, 11h41
  5. [DTD] Comment parser une DTD avec JDOM ?
    Par choko83 dans le forum Format d'échange (XML, JSON...)
    Réponses: 9
    Dernier message: 01/06/2007, 10h38

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