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 :
Je veux récupérer la valeur du tag "id" dans la balise proteinInteractor.
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>>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>
Voici mon code :
Or, malheureusement, je pars en pointeur null au moment du System.out.println dans la méthode listChildren.
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); } } }
Où est mon erreur ?
Merci de vos conseils !
@ ++
Partager