欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

使用dom4j的xpath解析xlm文件

程序员文章站 2022-05-28 21:08:59
...
package com.njusc.xmlTest;

import java.io.File;
import java.util.Iterator;
import java.util.List;

import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;

public class TestDom4jReadExmple
{
/*
* 创建一个测试xml bicashy.xml
*
<?xml version="1.0" encoding="UTF-8"?>
<students>
<student age="25">
<name>云天河</name>
<college>野外学院</college>
<telephone>62354666</telephone>
<notes>一个从小在山野里长大的少年,双亲早亡,小小年纪就开始了自立的生活。</notes>
</student>

<student>
<name>韩菱纱</name>
<college leader="侠盗高飞">盗墓学院</college>
<telephone>62358888</telephone>
<notes>女,出身于盗墓世家,却对辨识墓穴位置、**诡异机关、地脉风水之学颇有研究。</notes>
</student>
<student age="18">
<name>柳梦璃</name>
<college leader="">名门贵族</college>
<telephone>66666666</telephone>
<notes>梦璃是一个满怀柔情的女孩子,但她最温柔的一面只会留给自己喜欢的人。</notes>
</student>
<student age="18">
<name>慕容紫英</name>
<college>昆仑学院</college>
<telephone>88888888</telephone>
<notes>外冷内热、嫉恶如仇的热血青年,不但剑术超群,铸剑之术更是厉害。</notes>
</student>
</students>
*/


public static void main(String[] args) {
try{
//获取解析完后的解析信息
//利用XPath操作XML文件,获取想要的属性值


SAXReader saxReader = new SAXReader();
Document document = saxReader.read(new File("d:/bicashy.xml"));

/*直接定位到student name为云天河的节点的age属性*/
List list = document.selectNodes("/students/student[name=\"云天河\"]/@age");
Iterator iter = list.iterator();
if (iter.hasNext()) {
Attribute attribute = (Attribute) iter.next();
System.out.println("云天河-"+attribute.getName()+":"+attribute.getValue());
}


/*获取学生姓名为"韩菱纱"所在的学院名称*/
list = document.selectNodes("/students/student[name=\"韩菱纱\"]/college");
iter = list.iterator();
if (iter.hasNext()) {
Element element = (Element) iter.next();
String name = element.getName();
String value = element.getText();
System.out.println("韩菱纱-"+name+":"+value);
}


/*获取学生姓名为"韩菱纱"所在学院的师傅*/
list = document.selectNodes("/students/student[name=\"韩菱纱\"]/college/@leader");
iter = list.iterator();
if (iter.hasNext()) {
Attribute attribute = (Attribute) iter.next();
System.out.println("韩菱纱-college-"+attribute.getName()+":"+attribute.getValue());
}

/*获取学生姓名为"柳梦璃"的简介*/
list = document.selectNodes("/students/student[name=\"柳梦璃\"]/notes");
iter = list.iterator();
if (iter.hasNext()) {
Element element = (Element) iter.next();
System.out.println("柳梦-"+element.getName()+":"+element.getText());
}

/*获取年龄为"18"的学生的姓名、学院和电话号码*/
list = document.selectNodes("/students/student[@age='18']");
for(Object o:list){
Node n = (Node ) o;
Element name = (Element)n.selectObject("name");
Element college = (Element)n.selectObject("college");
Element telephone = (Element)n.selectObject("telephone");
System.out.println(name.getText()+"__ "+"college:"+college.getText()
+" telephone:"+telephone.getText());
}

/*获取年龄为"18",college的leader为空的学院名称*/
list = document.selectNodes("/students/student[@age='18']/college[@leader='']");
iter = list.iterator();
if (iter.hasNext()) {
Element element = (Element) iter.next();
System.out.println(""+element.getName()+":"+element.getText());
}


/*获取年龄为"18"姓名为"柳梦璃"、姓名、学院和电话号码*/
list = document.selectNodes("/students/student[@age='18'][name='柳梦璃']");
for(Object o:list){
Node n = (Node ) o;
Element name = (Element)n.selectObject("name");
Element college = (Element)n.selectObject("college");
Element telephone = (Element)n.selectObject("telephone");
System.out.println(name.getText()+"__ "+"college:"+college.getText()
+" telephone:"+telephone.getText());
}

/*获取年龄大于18的人的 姓名、学院和电话号码*/
list = document.selectNodes("/students/student[@age>18]");
for(Object o:list){
Node n = (Node ) o;
Element name = (Element)n.selectObject("name");
Element college = (Element)n.selectObject("college");
Element telephone = (Element)n.selectObject("telephone");
System.out.println(name.getText()+"__ "+"college:"+college.getText()
+" telephone:"+telephone.getText());
}

/*获取student所有的人的 姓名、学院和电话号码 */
//注 /students/student 可简写为//student根节点可以省去
list = document.selectNodes("//student");
for(Object o:list){
Node n = (Node ) o;
Element name = (Element)n.selectObject("name");
Element college = (Element)n.selectObject("college");
Element telephone = (Element)n.selectObject("telephone");
System.out.println(name.getText()+"__ "+"college:"+college.getText()
+" telephone:"+telephone.getText());
}

}catch(Exception ex){
ex.printStackTrace();
}
}
}



完成上述操作需要两个jar包