<?xml version="1.0" encoding="gb2312"?>
<books>
<book email="zhoujunhui">
<name>rjzjh</name>
<price>60.0</price>
</book>
</books>
import java.io.IOException;
import java.util.*;
import org.jdom.*;
import org.jdom.input.SAXBuilder;
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
SAXBuilder sb=new SAXBuilder(false);
Document doc = null;
try {
doc = sb.build("test1.xml");
} catch (JDOMException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Element root=doc.getRootElement(); //获取根元素
List list=root.getChildren("book");//取名字为book的所有元素
for(int i=0;i<list.size();i++){
Element element=(Element)list.get(i);
String email=element.getAttributeValue("email");
// String name=element.getChildText("name");
String name=element.getChildTextTrim("name");
String price=element.getChildText("price");
System.out.println("name:"+name);
System.out.println("price:"+price);
System.out.println("email:"+email);
}
}
}