使用jdom解析xml
package com.dowebber.jdomreader;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
public class JdomXMLReader {
public static void main(String[] args) {
String path = "d:\\students.xml";
Document doc = null;
try {
doc = new SAXBuilder().build(path);
} catch (JDOMException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("根标签: " + doc.getRootElement().getName());
Element root = doc.getRootElement();
List<Element> students = root.getChildren();
Iterator it = students.iterator();
while(it.hasNext()){
Element student = (Element) it.next();
if("student".equals(student.getName())){
Element name = student.getChild("name");
Element sex = student.getChild("sex");
System.out.print("name: " + name.getText());
System.out.println(" sex: " + sex.getText());
}
}
}
}