Querying elements in XML using XPath syntax in Java
1. Query the XML file for the Student.xml file
<?xml version= "1.0" encoding= "UTF-8"?>
<students>
<student class= "1201" >
<name>-Bruce Lee from </name>
<sex> Men </sex>
<age>25</age>
</student>
<student class= "1201" >
<name> Lin Chi-ling </name>
<sex> Women </sex>
<age>25</age>
</student>
<student class= "1201" >
<name> Lio Nio </name>
<sex> Men </sex>
<age>25</age>
</student>
<student class= "1302" >
<name> Zhang June </name>
<sex> Men </sex>
<age>25</age>
</student>
<student class= "1302" >
<name> Ning </name>
<sex> Men </sex>
<age>25</age>
</student>
<student class= "1302" >
<name> Lin Zhidong </name>
<sex> Men </sex>
<age>25</age>
</student>
<student class= "1302" >
<name> cuisine Xiao Feng </name>
<sex> Men </sex>
<age>25</age>
</student>
<student class= "1302" >
<name> Jolin Tsai </name>
<sex> Women </sex>
<age>25</age>
</student>
</students>
2. Use the XPath query steps as follows:
2.1 Create an instance of Xpathfactory:
Xpathfactory XF = new Xpathfactory ();
2.2 Get an XPath instance from an instance of Xpathfactory:
XPath x = Xf.newxpath ();
2.3 Query the XML using the evaluate () method of the XPath instance:
2.3.1 now cite the following two methods.
Specific Link: http://docs.oracle.com/javase/8/docs/api/javax/xml/xpath/XPath.html
/*
Expression: The query statement for the XPath syntax.
Source: The origin of the *.xml of the input query
ReturnType: The types of returned results are: Xpathconstants.node (single Element), Xpathconstants.nodeset (collection of elements)
Specific return type link: 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 Create a new Selectxml class:
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 ();
The Student.xml file is in the SRC directory.
InputStream in = SelectXML.class.getResourceAsStream ("/student.xml");
InputSource Source = new InputSource (in),//inputsource is closed once used, and re-used again with new.
Specific links to XPath syntax: http://www.w3school.com.cn/xpath/xpath_syntax.asp
String query_sentences= "/students/student/[name= '-Bruce Lee away from ']/age".
Inquire about his age by name "-Bruce Lee".
string evaluate (string Expression,inputsource source);
String age = (Node) path.evaluate (query_sentences, source);
Sysotem.out.println ("The Age of Li Xiao is:" + ages);
}
}
2.4.1 Create a new Datastore class called the Student class: This class is used to encode a Java object into an XML file or to decode an XML file into a Java object
Import Javax.xml.bind.annotation.XmlAttribute;
Import javax.xml.bind.annotation.XmlRootElement;
@XMLRootElement (name= "student")//inject annotations, marking the root element in the class as student.
public class student{
The attribute of the @XmlAttribute (Name= "class")//tag student is class.
Private Clazz;
private String name;
Private String sex;
Private String age;
Set,get,tostring () Province column.
}
2.4.2 Create a new Datastore class called the Studentlist class: This class is used to encode a Java object into an XML file or to decode an XML file into a Java object
Import java.util.List;
Import javax.xml.bind.annotation.XmlElement;
Import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement (name= "Students")//inject annotations, marking the root element in the class as students.
public class studentlist{
Private list<student> students;
Public list<student> getstudents () {return students; }
@XmlElement (name= "student")//inject annotations, marking the element in the class as student.
public void Setstudents (list<student> students) {this.students = students; }
}
2.4.2 Create a new Selectxml class:
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);
Queries all students for all information and returns a collection.
NodeList Student_node = (NodeList) path.evaluate (query_sentences, Source,xpathconstants.nodeset);
Queries all information about a student and returns an element.
Node Student_node = (node) path.evaluate (Query_sentences, Source,xpathconstants.node);
return student_node;
}
Use the Jaxbcontext->unmarshaller () method to decode the student.xml.
Specific Link: http://docs.oracle.com/javase/8/docs/api/javax/xml/bind/Unmarshaller.html
Public list<student> Getunmarshallerjava (NodeList NodeList) throws jaxbexception{
Creates a decoder.
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);
to decode.
Student stu = (Student) u.unmarshal (node);
List.add (Stu);
}
return list;
}
Use the Jaxbcontext->marshaller () method to encode an object in Java.
Specific Link: http://docs.oracle.com/javase/8/docs/api/javax/xml/bind/marshaller.html
public static void Getmarshallerxml (List<student> List) throws Xpathexpressionexception, jaxbexception{
Creating encoders
Jaxbcontext JC = jaxbcontext.newinstance (Studentlist.class);
Marshaller u = Jc.createmarshaller ();
Studentliststu = new Studentlist ();
Stu.setstudents (list);
Executes the encoding to create an XML file on the hard disk.
U.marshal (Stu, New File ("D:" +file.separator+ "Student.xml"));
}
}
Java uses XPath to query elements in XML