读取xml文件基于xpath
关键字: 读取xml文件利用xpath
http://wjjbfq.javaeye.com/blog/411879
- <students>
- <student province="Jiangsu">
- <id>110</id>
- <name>wangzhen</name>
- <major>English</major>
- <address province="guangzhou">Guangdong</address>
- <gender>male</gender>
- <birth>1987-08-08</birth>
- </student>
- <student province="shanxi">
- <id>111</id>
- <name>wangzhen</name>
- <major>English</major>
- <address province="guangzhou">Guangdong</address>
- <gender>male</gender>
- <birth>1987-12-08</birth>
- </student>
- </students>
1、 查找所有学生名字为wangzhen的birth
- String xpath="//student[name='wangzhen']/birth";
- List<Node> birthList= document.selectNodes(xpath);
- for(Node address :birthList){
- System.out.println(address.getText());
- }
获得节点的值一般用getText() 获得属性的值用getValue()
2、查找student下province属性值为shanxi的学生id
- String xpath = "//student[@province='shanxi']";
- //得到student元素province属性值为shanxi的所有学生元素
- List<Element> studentElementList = document.selectNodes(xpath);
- //循环获得单个学生元素。
- for(Element studentElement:studentElementList){
- //获得学生元素的id节点的值。其它值也可以这样获取。
- String id =studentElement.elementText("id");
- System.out.println(id);
- }
作者:wenhai_zhang 发表于2009-11-25 23:44:00 原文链接
阅读:27 评论:0 查看评论