package com.imooc.Dom4jTest; import java.io.File; import java.util.Iterator; import java.util.List; import org.dom4j.Attribute; import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.Element; import org.dom4j.io.SAXReader; public class DOM4JTest { public static void main(String[] args) { SAXReader reader = new SAXReader(); try { Document document = reader.read(new File("src/res/books.xml")); Element bookStore = document.getRootElement(); Iterator it = bookStore.elementIterator(); while (it.hasNext()) { System.out.println("------------开始遍历某本书---------------"); Element book = (Element) it.next(); List<Attribute> bookAttrs = book.attributes(); for (Attribute attr : bookAttrs) { System.out.println("属性名:" + attr.getName() + "------属性值" + attr.getValue()); } Iterator itt = book.elementIterator(); while (itt.hasNext()) { Element bookChild = (Element) itt.next(); System.out.println("节点名:" + bookChild.getName() + "----节点值:" +bookChild.getStringValue()); } System.out.println("------------结束遍历某本书---------------"); } } catch (DocumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
HOTLINE
相关分类