xpath遍历xml节点
import java.io.File;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;
public class XmlXPath {
public static void main(String args[]) throws Exception{
SAXReader reader = new SAXReader();
Document doc = reader.read(new File("chenhuan.xml"));
/*通过xpath找到特定的节点*/
List<Node> list = doc.selectNodes("//hibernate-mapping/class");
for(Node n : list){
System.out.println("拿到标签的名称:"+n.getName());
System.out.println("拿到标签的name值:"+n.valueOf("@name"));
}
/*获取特定节点中的第一个节点*/
Node node = doc.selectSingleNode("//hibernate-mapping/class");
System.out.println("拿到标签的名称:"+node.getName());
System.out.println("拿到标签的name值:"+node.valueOf("@name"));
}
}