xml解析
dom4j解析技术
步骤:
- 读取books.xml对象
- 通过dom对象获取根元素
- 通过根元素获取标签对象 element() elements() 通过标签名查找子元素
- 遍历 调用asXML()方法将标签对象转化为字符串
SAXReader saxReader = new SAXReader();
Document document = saxReader.read("src/clothes");
System.out.println("document对象是:"+document);
Element rootElement = document.getRootElement();
List<Element> clothes = rootElement.elements("cloth");
for (Element cloth : clothes){
System.out.println(cloth.asXML()); // asXML()将标签对象转化为标签字符串
}
<?xml version="1.0" encoding="utf-8"?>
<clothes>
<cloth no="3191164022">
<brand>Cabbeen</brand>
<type>卫衣</type>
<name>蝴蝶刺绣</name>
<price>659</price>
</cloth>
<cloth no="3193126513">
<brand>2AM</brand>
<type>工装裤</type>
<name>多口袋束脚</name>
<price>599</price>
</cloth>
</clothes>