我正在学习一个教程,并遇到了一个我预计会起作用的问题。我希望有人可以帮助我处理这段代码。我过不了这个。如果我使用 XPATH 表达式“ //*/text() ”,我在下面共享的这段代码将匹配,但是当我得到比这更具体并使用“ //tag0:G/text() ”时它无法匹配。知道我做错了什么吗?我只是想从下面提供的 XML 中获取 2 个“tag0:G”值:
import java.io.IOException;
import java.io.StringReader;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
public class Test1 {
public static void main(String[] args) {
System.out.println("Test start...");
String myXML = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
"<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\">" +
"<soapenv:Body>" +
"<tag0:getA xmlns:tag0=\"http://me.ws.ix\">" +
"<tag0:B>" +
"<tag0:CC>" +
"<tag0:CC>" +
"<tag0:D>false</tag0:D>" +
"<tag0:E>false</tag0:E>" +
"<tag0:F xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:nil=\"true\"/>" +
"<tag0:G>10001</tag0:G>" +
"<tag0:H>7744000002</tag0:H>" +
"</tag0:CC>" +
"<tag0:CC>" +
"<tag0:D>false</tag0:D>" +
"<tag0:E>false</tag0:E>" +
"<tag0:F xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:nil=\"true\"/>" +
"<tag0:G>20002</tag0:G>" +
"<tag0:H>1111122222</tag0:H>" +
"</tag0:CC>" +
"</tag0:CC>" +
"<tag0:I>2012-05-27 23:38:48</tag0:I>" +
"</tag0:B>" +
"</tag0:getA>" +
"</soapenv:Body>" +
"</soapenv:Envelope>";
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
Document doc = null;
NodeList nodes = null;
try {
doc = factory.newDocumentBuilder().parse( new InputSource( new StringReader( myXML) ) );
XPathExpression expr = XPathFactory.newInstance().newXPath()
.compile("//tag0:G/text()"); // this fails, I don't know why
nodes = (NodeList)expr.evaluate(doc, XPathConstants.NODESET);
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (XPathExpressionException e) {
e.printStackTrace();
}
System.out.println("Nodes length: " + nodes.getLength() );
for (int i = 0; i < nodes.getLength(); i++) {
String val = nodes.item(i).getNodeValue();
System.out.println( "Val: " + val );
}
System.out.println("Test end...");
}
}