/**
* 利用Dom4j API 读取XML文件
*/publicstaticvoidmain(String[] args)throwsDocumentException,FileNotFoundException{//books.xml 位置在项目文件夹中String file ="Books.xml";FileInputStream in =newFileInputStream(file);SAXReader reader =newSAXReader();//reader方法从流中读取byte,并且解析为Document对象(称为文档对象dom);Document doc = reader.read(in);//检查文档对象doc内容System.out.println(doc.asXML());//找到唯一的根元素booksElement root = doc.getRootElement();System.out.println(root.asXML());//从root中招到全部的book子元素List<Element> list = root.elements();//返回全部子元素List<Element> list1 = root.elements("book");//返回全部bookfor(Element e : list1){System.out.println(e.asXML());}for(Element e : list1){// System.out.println(e.asXML());//找到book元素中第一个name子元素Element name = e.element("name");//getText可以获取元素中的文本内容String text = name.getText();System.out.println(text);}//读取book元素的属性String id = e.attributeValue("id");System.out.println(id);//直接获取book元素中的文本内容String n = e.elementText("name");System.out.println(n);}