user1167910
user1167910

Reputation: 145

parsing xml file by dom4j

I am parsing a xml file by dom4j. Here's what the file looks like:

<bug>
<bug_id>1232131<bug_id>
<long_desc isprivate="0">
<who name="Alan John">[email protected]</who>
<thetext>asdasdasdsadasdasdasd
asdasdad
adasd
adinadasbdk
asdasdad</thetext>
</long_desc>

<long_desc isprivate="0">
<who name="Bob Dan">[email protected]</who>
<thetext>asdasdasdsadasdasdasd
asdasdadads
adasdojojjtjghjthnjthntjhnjthn
adinadasbdk
asdasdad</thetext>
</long_desc>

</bug>

There are several long_desc in one bug tag, also there are several bug tag in the xml file.

I used dom4j to print the content in but I failed here's my code.

File f = new File("c:/Users/ah/bugs01.xml"); 
    SAXReader reader = new SAXReader(); 
    Document doc = reader.read(f); 
    Element root = doc.getRootElement(); 
    Element foo; 
    for (Iterator i = root.elementIterator("bug"); i.hasNext();) { 
        foo = (Element) i.next();
        System.out.println("Text" + foo.elementText("thetext"));
        } 

But I got null for each println. Why? I am new to dom4j and I hope someone can help me solve this problem. Thanks a lot

Upvotes: 1

Views: 136

Answers (1)

Chris Knight
Chris Knight

Reputation: 25074

Your XML isn't valid:

<thetext>asdasdasdsadasdasdasd
asdasdadads
adasdojojjtjghjthnjthntjhnjthn
adinadasbdk
asdasdad</text>

has a different start (<thetext>) and end (</text>) tag. Also, thetext is a child of long_desc, not bug.

Upvotes: 2

Related Questions