/**
*
*/
package com.xcj.test;
import java.io.File;
import java.util.Iterator;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
/**
* @author xcj
*
*/
public class xmls {
/**
* <b>function:</b>
* @project xcjedu
* @package com.xcj.test
* @fileName @param args
* @createDate May 30, 2014 6:13:54 PM
* @author yy.niu
*/
public static void main(String[] args) {
try {
SAXReader sax = new SAXReader();
Document xmlDoc = sax.read(new File("E:\\xcjChapter.xml"));
Element root = xmlDoc.getRootElement();//根节点
Element name = (Element) root.element("name");
Element courseNumber = (Element) root.element("courseNumber");
System.out.println("name"+name.getText());
System.out.println("courseNumber"+courseNumber.getText());
System.out.println("-------------------------------------------");
List list = root.selectNodes("//chapter");
for(int i = 0; i < list.size(); i++){
Element pro = (Element)list.get(i);
System.out.println(pro.getName());
System.out.println(pro.attributeValue("id"));
System.out.println(pro.attributeValue("url"));
System.out.println(pro.getText());
for(Iterator iterInner = pro.elementIterator(); iterInner.hasNext();){
Element elementInner = (Element) iterInner.next();
System.out.println(elementInner.getName());
System.out.println(elementInner.attributeValue("id"));
System.out.println(elementInner.attributeValue("url"));
System.out.println(elementInner.getText());
for(Iterator iterpag = elementInner.elementIterator(); iterpag.hasNext();){
Element elementpag = (Element) iterpag.next();
System.out.println(elementpag.getName());
System.out.println(elementpag.attributeValue("id"));
System.out.println(elementpag.attributeValue("url"));
System.out.println(elementpag.getText());
System.out.println("+++++++++++++++++++++++++++++++++");
}
}
}
} catch (DocumentException e) {
e.printStackTrace();
}
}
}
附件是解析的xml