问题描述
selectSingleNode(NodecontextNode,java.lang.Stringstr)我以为它会从contextNode这个节点开始往下搜索,结果总是从xml文件的开头开始搜索。不知道是我哪里弄错了。。。请高手指点。原程序如下:importjava.io.File;importjavax.xml.parsers.DocumentBuilder;importjavax.xml.parsers.DocumentBuilderFactory;importorg.w3c.dom.Document;importorg.w3c.dom.Node;importorg.w3c.dom.NodeList;importcom.sun.org.apache.xpath.internal.XPathAPI;publicclassXPathAPITest{/***@paramargs*/publicstaticvoidmain(String[]args){test();}publicstaticvoidtest(){DocumentBuilderFactoryfactory;DocumentBuilderbuilder;Documentdoc;try{factory=DocumentBuilderFactory.newInstance();builder=factory.newDocumentBuilder();doc=builder.parse(newFile("test.xml"));NodeListlist=XPathAPI.selectNodeList(doc,"//root");for(inti=0;i<list.getLength();i++){Nodenode=list.item(i);System.out.println(XPathAPI.selectSingleNode(node,"//root/sub").getTextContent());}}catch(Exceptionex){ex.printStackTrace();}}}xml文件和相关的xsd文件如下:<?xmlversion="1.0"encoding="UTF-8"?><rootsxmlns="http://www.example.org/test"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.example.org/testtest.xsd"><root><sub>sub11</sub><sub>sub12</sub><sub>sub13</sub></root><root><sub>sub21</sub><sub>sub22</sub><sub>sub23</sub></root></roots><?xmlversion="1.0"encoding="UTF-8"?><schemaxmlns="http://www.w3.org/2001/XMLSchema"targetNamespace="http://www.example.org/test"xmlns:tns="http://www.example.org/test"elementFormDefault="qualified"><elementname="roots"><complexType><sequence><elementref="tns:root"></element></sequence></complexType></element><elementname="root"><complexType><sequence><elementname="sub"type="string"minOccurs="1"maxOccurs="unbounded"></element></sequence></complexType></element></schema>希望得到的输出是:sub11sub21但是老是输出:sub11sub11