I'm trying to get the childnode's attribute from my xml file.For example,the file's name is bb.xml
,
The xml is just like this:
<?xml version="1.0" encoding="utf-8"?>
<aaa>
<bbb name="MainMenu">
<ccc name="abc" Classes="1"
A="2"
B="3"/>
</bbb>
<bbb name="Mainwindow">
<ccc name="abc" Classes="4"
A="3"
B="2"/>
</bbb>
</aaa>
and main java file is this:
public static void main(String[] args) throws Exception {
Document doc = new SAXReader().read(new File("D:/bb.xml"));
List itemList = doc.selectNodes("/aaa");
for (Iterator iter=itemList.iterator(); iter.hasNext();) {
Element el = (Element)iter.next();
String name = el.attributeValue("name");
System.out.println("name:"+name);
String b = el.attributeValue("B");
System.out.println("b:"+b);
}
I get the result in my console:
name:null
b:null
But I want the result is:
name:MainMenu
b:3
name:Mainwindow
b:2
How can figure it out?
Quick and dirty:
A better solution (because of flexibility) is to use Java native
org.w3c.dom
package. That way the solution would look like this:In any case: don't forget the closing
</aaa>
in your xml.