<books>
<book id="001">
<title>Harry Potter</title>
<author>J K. Rowling</author>
</book>
<book id="002">
<title>Learning XML</title>
<author>Erik T. Ray</author>
</book>
</books>
package org.liukai.tutorial.Xml;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Iterator;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
public class Test1 {
public static void main(String[] args) {
SAXReader sax=new SAXReader();
try {
Document doc = sax.read(new FileInputStream(new File("C:\\Users\\admin\\Desktop\\text.xml")));
Element rootElement = doc.getRootElement();
Iterator it = rootElement.elementIterator();
while(it.hasNext()){
Element e = (Element) it.next();
System.out.println(e.elementText("title"));
System.out.println(e.attributeValue("id"));
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}