标签:
使用java中的xpath语法查询xml中元素
1.查询的xml文件为student.xml文件
<?xml version="1.0" encoding="UTF-8" ?>
<students>
<student class="1201">
<name>李小离</name>
<sex>男</sex>
<age>25</age>
</student>
<student class="1201">
<name>林志玲</name>
<sex>女</sex>
<age>25</age>
</student>
<student class="1201">
<name>廖新峰</name>
<sex>男</sex>
<age>25</age>
</student>
<student class="1302">
<name>张军</name>
<sex>男</sex>
<age>25</age>
</student>
<student class="1302">
<name>任峰</name>
<sex>男</sex>
<age>25</age>
</student>
<student class="1302">
<name>林志东</name>
<sex>男</sex>
<age>25</age>
</student>
<student class="1302">
<name>菜晓峰</name>
<sex>男</sex>
<age>25</age>
</student>
<student class="1302">
<name>蔡依林</name>
<sex>女</sex>
<age>25</age>
</student>
</students>
2.使用XPath查询步骤如下:
2.1创建一个XPathFactory的实例:
XPathFactory xf = new XPathFactory();
2.2通过XPathFactory的实例获取一个XPath实例:
XPath x = xf.newXPath();
2.3通过XPath实例的evaluate()方法对XML进行查询:
2.3.1 现在例举以下两个方法。
具体链接:http://docs.oracle.com/javase/8/docs/api/javax/xml/xpath/XPath.html
/*
expression:为XPath语法的查询语句。
source :输入查询的*.xml的来源
returnType:返回结果的类型有:XPathConstants.NODE(单个元素),XPathConstants.NODESET(元素的集合)
具体的返回类型链接:http://docs.oracle.com/javase/8/docs/api/javax/xml/xpath/XPathConstants.html
String evaluate(String expression,InputSource source);
Object evaluate(String expression,InputSource source,return returnType);
*/
2.3.1新建一个SelectXML类:
import java.io.InputStream;
import org.xml.sax.InputSource;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
public class SelectXML{
public void getSelectXML( String query_sentences) throws XPathExpressionException{
XPathFactory pathFactory = XPathFactory.newInstance();
XPath path = pathFactory.newXPath();
//该student.xml文件为于src目录下。
InputStream in = SelectXML.class.getResourceAsStream("/student.xml");
InputSource source = new InputSource(in);//InputSource使用一次就会关闭,再次使用就用再new一次。
//XPath语法的具体链接:http://www.w3school.com.cn/xpath/xpath_syntax.asp
String query_sentences="/students/student/[name=‘李小离’]/age"。
//通过姓名为“李小离”查询他的年龄。
//String evaluate(String expression,InputSource source);
String age = (Node)path.evaluate( query_sentences, source);
sysotem.out.println("李小离的年龄是: "+ age);
}
}
2.4.1新建一个数据存储类叫Student类:该类是用来将Java对象编码成为XML文件或是xml文件解码成为Java对象
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlRootElement;
@XMLRootElement(name="student")//注入注解,标记在该类中根元素为student。
public class Student{
@XmlAttribute(name="class")//标记student的属性是class.
private clazz;
private String name;
private String sex;
private String age;
//set,get,tostring()省列。
}
2.4.2新建一个数据存储类叫StudentList类:该类是用来将Java对象编码成为XML文件或是xml文件解码成为Java对象
import java.util.List;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name="students") //注入注解,标记在该类中根元素为students。
public class StudentList{
private List<Student> students;
public List<Student> getStudents() { return students; }
@XmlElement(name="student") //注入注解,标记在该类中元素为student。
public void setStudents(List<Student> students) { this.students = students; }
}
2.4.2新建一个SelectXML类:
import java.io.InputStream;
import org.xml.sax.InputSource;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
public class SelectXML{
public NodeList getSelectXML( String query_sentences) throws XPathExpressionException{
XPathFactory pathFactory = XPathFactory.newInstance();
XPath path = pathFactory.newXPath();
InputStream in = SelectXML.class.getResourceAsStream("/student.xml");
InputSource source = new InputSource(in);
String query_sentences = "/students/student"
//String query_sentences = "/students/student[@class="1301"]"
//Object evaluate(String expression,InputSource source,return retrunType);
//查询所有学生的所有信息,返回一个集合。
NodeList student_node = (NodeList)path.evaluate( query_sentences, source,XPathConstants.NODESET);
//查询一个学生的所有信息,返回一个元素。
//Node student_node = (Node)path.evaluate( query_sentences, source,XPathConstants.NODE);
return student_node;
}
//使用JAXBContext->unmarshaller()方法对student.xml进行解码。
//具体链接:http://docs.oracle.com/javase/8/docs/api/javax/xml/bind/Unmarshaller.html
public List<Student> getUnmarshallerJAVA(NodeList nodelist) throws JAXBException{
//创建解码器。
JAXBContext jc = JAXBContext.newInstance(Student.class);
Unmarshaller u = jc.createUnmarshaller();
List<Student> list = new LinkedList<>();
for(int i = 0;i<nodelist.getLength();i++){
Node node = nodelist.item(i);
//进行解码。
Student stu =(Student)u.unmarshal(node);
list.add(stu);
}
return list;
}
//使用JAXBContext->marshaller()方法对Java中的对象进行编码。
////具体链接:http://docs.oracle.com/javase/8/docs/api/javax/xml/bind/marshaller.html
public static void getMarshallerXML(List<Student> list) throws XPathExpressionException, JAXBException{
// 创建编码器
JAXBContext jc = JAXBContext.newInstance(StudentList.class);
Marshaller u = jc.createMarshaller();
StudentListstu = new StudentList();
stu.setStudents(list);
// 执行编码,在硬盘上创建xml文件。
u.marshal(stu, new File("D:"+File.separator+"student.xml"));
}
}
标签:
原文地址:http://www.cnblogs.com/ljhai/p/4792863.html