一、依赖
<dependency><groupId>dom4j</groupId><artifactId>dom4j</artifactId><version>1.6.1</version></dependency>
二、test.xml
<?xml version="1.0" encoding="UTF-8"?>
<students><student><name>lipiao</name><birthday>1997-08-07</birthday><sg description="身高">155</sg></student><student><name>cc</name><birthday>1995-10-18</birthday><sg description="身高">155</sg></student>
</students>
三、解析xml数据
package dom4j_demo;import java.util.List;import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;public class dom4j {public static void main(String[] args) throws DocumentException {SAXReader reader = new SAXReader();Document document = reader.read("C:\\Users\\cc\\Desktop\\test.xml");Element rootElement = document.getRootElement();//读取根结点System.out.println("结点标签名:"+rootElement.getName());//结点标签名System.out.println("结点属性值:"+rootElement.attributeValue("att"));//结点属性值List<Element> studentElementList = rootElement.elements("student");for (Element element : studentElementList) {System.out.println(element.getName());System.out.println(element.getStringValue());Element sgElement = element.element("sg");System.out.println("结点属性值"+sgElement.attributeValue("description"));System.out.println(element.element("name").getText()+"身高:"+sgElement.getText());}}}