Bonjour a tous,
j'ai grandement besoin de votre aide.
j'ai ce message d'erreur lorsque j'essai de recuperer le contenu dans la balise comment.
Merci d'avance
Exception in thread "main" java.lang.NullPointerException
at JDOM2.afficheALL(JDOM2.java:42)
at JDOM2.main(JDOM2.java:26)
voici mon xml:
et 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 <FinancialConceptInfo name="CANCELLABLESWAP_A" type="Deal"> <Rules> </Rules> <Drools> </Drools> <Concept> <FinancialConcepts xmlns="" xmlns:xsi="http://www.xxx.../XMLSchema-instance" xsi:noNamespaceSchemaLocation="FinancialConcept.xsd"> <FinancialConcept father="SINGLESWAP_A" fatherVersion="2" isReadOnly="false" name="CANCELLABLESWAP_A" type="Deal" version="2"> <Comment> @![CDATA[Cancellable Swap allows to pay a fixed rate and receive a floating rate (and vice-versa), the terms and conditions allow to cancel (call) the deal at predefined dates (usually matching with coupon payments date) Structured Leg supporting: - Upfront fee - Fixed/CMS/Libor rate - Spread of Libor/Spread of CMS - Cap Floor - Inverse floater ( M% - rate ) Funding Leg supporting: - Upfront fee - Fixed/CMS/Libor rate - Cap Floor Options dates supporting: - Generation from schedule or funding leg Upfront Solver to compute upfront fee Step-up/down of rate manageable in the schedules]]@ </Comment> </FinancialConcept>
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; public class JDOM2 { static org.jdom.Document document; static Element racine; public static void main(String[] args) { //On crée une instance de SAXBuilder SAXBuilder sxb = new SAXBuilder(); try { //On crée un nouveau document JDOM avec en argument le fichier XML //Le parsing est terminé ;) document = sxb.build(new File("test.xml")); } catch(Exception e){} //On initialise un nouvel élément racine avec l'élément racine du document. racine = document.getRootElement(); //Méthode définie dans la partie 3.2. de cet article afficheALL(); } //Ajouter cette méthodes à la classe JDOM2 static void afficheALL() { //On crée une List contenant tous les noeuds "concept" de l'Element racine List listConcept = racine.getChildren("Concept"); //On crée un Iterator sur notre liste Iterator i = listConcept.iterator(); while(i.hasNext()) { //On recrée l'Element courant à chaque tour de boucle afin de //pouvoir utiliser les méthodes propres aux Element comme : //selectionner un noeud fils, modifier du texte, etc... Element courant = (Element)i.next(); //On affiche le nom de l'element courant System.out.println(courant.getChild("comment").getText()); } } }
Partager