XML文件数据:
package com.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;
public class ReadXML {
public static void main(String[] args) {
ReadXML xml=new ReadXML();
try {
xml.readXMLfile("D:\\city.xml");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void readXMLfile(String filePath) throws Exception{
File xmlFile=new File(filePath);
SAXReader reader=new SAXReader();
Document xmlDoc=reader.read(xmlFile); // Read xml stream
Element root=xmlDoc.getRootElement(); //Get the root node of XML File
int j=0;
List listRowSet=xmlDoc.selectNodes("//country[1]//area//city");
for(Iterator i=listRowSet.iterator();i.hasNext();){
Element ele=(Element)i.next();
System.out.println("Test "+ele.getName());
j++;
}
}
}
关于selectNodes这个方法参数具体见
http://www.w3school.com.cn/xpath/xpath_syntax.asp