1、需要解析的XML文档内容如下: <?xml version="1.0" encoding="utf-8"?> <family> <father nativeplace="湖北武汉">张三</father> <wife nativeplace="浙江杭州">里斯</wife> <son>张帆</son> </family> 2、读取该文档的程序ReadXML.java package dom4j.sample; import java.util.Iterator; import org.dom4j.*; import org.dom4j.io.SAXReader; public class ReadXML { public static void main(String[] args) { readMyXML(); } public static void readMyXML() { SAXReader reader = new SAXReader();//创建SAX读取器 try { Document document = reader.read("test.xml"); //读入文档 Element root = document.getRootElement(); //获取根元素 Iterator<?> iterator = root.elementIterator();//迭代器 while (iterator.hasNext()) { //强制转换为元素类型 Element element = (Element) iterator.next(); System.out.print(element.getText());//打印输出元素内容 System.out.println("/t" + element.attributeValue("nativeplace"));//输出属性值 } } catch (DocumentException e) { e.printStackTrace(); } } }