package anby.com;import java.util.Iterator;public class Dom4JRead {public static void main(String[] args) throws DocumentException { //Dom4JReader(); SAXReader saxreader=new SAXReader(); Document doc=saxreader.read("Book.xml"); Node node =doc.selectSingleNode("//book[@id='boo1']");Element ele=(Element)node;String titile=ele.elementText("titile"); System.out.println(titile);//呐喊 }public static void Dom4JReader() throws DocumentException {SAXReader reader=new SAXReader();Document document=reader.read("Book.xml");//擷取dom到記憶體 Element root=document.getRootElement();//擷取根項目 Iterator i=root.elementIterator(); //使用迭代器while (i.hasNext()){ Element ele=(Element)i.next(); //迭代取出Element Attribute attribute= ele.attribute("id");//擷取id屬性 System.out.println( attribute.getValue()); //取出ele中的元素 Iterator it=ele.elementIterator(); while(it.hasNext()) { Element childEle=(Element)it.next();//迭代元素 System.out.println( childEle.getText());//擷取ChildEle中常值內容 }}}}//result://boo1//呐喊//18//boo2//彷徨//15