<?xml version="1.0" encoding="UTF-8"?>
<students>
<student id="001" xx="嘻嘻">
<name>林明</name>
<sex>男</sex>
<age>26</age>
</student>
<student id="002" xx="嘻嘻">
<name >黄亮</name>
<sex>男</sex>
<age>21</age>
</student>
</students>
package com.java.xml;
import java.util.Iterator;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
public class DOM4j02 {
public static void main(String[] args) throws Exception{
SAXReader saxReader=new SAXReader();
Document document=saxReader.read("src/Students.xml");
Element rootElement=document.getRootElement();
Iterator iter=rootElement.elementIterator();
while(iter.hasNext()){
Element studentElement=(Element)iter.next();
System.out.println("学号:"+studentElement.attributeValue("id"));
System.out.println("姓名:"+studentElement.elementText("name"));
System.out.println("性别:"+studentElement.elementText("sex"));
System.out.println("年龄:"+studentElement.elementText("age"));
System.out.println("=========");
}
}
}
学号:001
姓名:林明
性别:男
年龄:26
=========
学号:002
姓名:黄亮
性别:男
年龄:21
=========