The following math. xml file is used as the example XML file. The document shows the math score of an exam, which contains the score of student LZX and ZJ.
------------- Math. xml -------------
<Math>
<Student ID = "1">
<Name> ZJ </Name>
<Score> 87 </score>
</Student>
<Student ID = "2">
<Name> LZX </Name>
<Score> 85 </score>
</Student>
</Math>
-------------------------------------
Perform the following operations:
1. Read math. XML to memory
Documentbuilderfactory DBF = documentbuilderfactory.Newinstance();
Documentbuilder DB = DBF. newdocumentbuilder ();
File F =NewFile ("D:" "math. xml ");
Document Doc = dB. parse (f );
2. Obtain the student node list
Xpathfactory = xpathfactory.Newinstance();
XPath = xpathfactory. newxpath ();
Nodelist list = (nodelist) XPath. Evaluate ("Math/student", doc,
Xpathconstants.Nodeset);
3. Obtain the first node with student as the node.
Xpathfactory = xpathfactory.Newinstance();
XPath = xpathfactory. newxpath ();
Node node = (node) XPath. Evaluate ("Math/student", doc,
Xpathconstants.Node);
Complex functions can be implemented based on the DOM tree structure and XPath path.
XPath Syntax: http://www.w3school.com.cn/xpath/xpath_syntax.asp