jar:jdom.jar
测试类:
public class TestJdom
{
public static void main(String[] args)
{
SAXBuilder sa = new SAXBuilder();
try {
Document doc = sa.build(TestJdom.class.getClassLoader().getResourceAsStream("test.xml"));
Element element = doc.getRootElement();
List list = element.getChildren("person");
for(int i=0;i<list.size();i++)
{
Element e = (Element) list.get(i);
String name = e.getChildText("name");
String email = e.getChildText("email");
String country = e.getChildText("country");
System.out.println("name:"+ name);
System.out.println("email:"+ email);
System.out.println("country:"+ country);
System.out.println("--------------------");
}
} catch (JDOMException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Xml文件:test.xml
<?xml version="1.0" encoding="UTF-8"?>
<people>
<person>
<name>Ian Darwin</name>
<email>http://www.darwinsys.com/</email>
<country>中国</country>
</person>
<person>
<name>Another Darwin</name>
<email type="intranet">afd@node1</email>
<country>Canada</country>
</person>
</people>