[英]parsing xml file by dom4j
I am parsing a xml file by dom4j. 我正在解析dom4j的xml文件。 Here's what the file looks like:
该文件的外观如下:
<bug>
<bug_id>1232131<bug_id>
<long_desc isprivate="0">
<who name="Alan John">alanjohn@whatever.com</who>
<thetext>asdasdasdsadasdasdasd
asdasdad
adasd
adinadasbdk
asdasdad</thetext>
</long_desc>
<long_desc isprivate="0">
<who name="Bob Dan">bdan@whatever.com</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. 一个bug标签中包含多个long_desc,xml文件中也包含多个bug标签。
I used dom4j to print the content in but I failed here's my code. 我使用dom4j打印内容,但是我的代码失败了。
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. 但是我对于每个println都为空。 Why?
为什么? I am new to dom4j and I hope someone can help me solve this problem.
我是dom4j的新手,希望有人可以帮助我解决这个问题。 Thanks a lot
非常感谢
Your XML isn't valid: 您的XML无效:
<thetext>asdasdasdsadasdasdasd
asdasdadads
adasdojojjtjghjthnjthntjhnjthn
adinadasbdk
asdasdad</text>
has a different start ( <thetext>
) and end ( </text>
) tag. 具有不同的开始(
<thetext>
)和结束( </text>
)标记。 Also, thetext
is a child of long_desc
, not bug
. 此外,
thetext
是一个孩子long_desc
,没有bug
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.