Java之Dom4J(Read )
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