java中命名空间xpath的输出
我有下面的代码,在特定字段及其输出方面遇到了一些问题。名称空间已连接,但似乎未在必填字段上输出。这方面的任何信息都会很好java中命名空间xpath的输出,java,string,xpath,field,Java,String,Xpath,Field,我有下面的代码,在特定字段及其输出方面遇到了一些问题。名称空间已连接,但似乎未在必填字段上输出。这方面的任何信息都会很好 import org.w3c.dom.Document; import org.xml.sax.SAXException; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.ParserConf
import org.w3c.dom.Document;
import org.xml.sax.SAXException;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPathFactory;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathExpressionException;
import java.io.ByteArrayInputStream;
import java.io.IOException;
public class test
{
public static void main(String args[])
{
String xmlStr = "<aws:UrlInfoResponse xmlns:aws=\"http://alexa.amazonaws.com/doc/2005-10-05/\">\n" +
" <aws:Response xmlns:aws=\"http://awis.amazonaws.com/doc/2005-07-11\">\n" +
" <aws:OperationRequest>\n" +
" <aws:RequestId>blah</aws:RequestId>\n" +
" </aws:OperationRequest>\n" +
" <aws:UrlInfoResult>\n" +
" <aws:Alexa>\n" +
" <aws:TrafficData>\n" +
" <aws:DataUrl type=\"canonical\">harvard.edu/</aws:DataUrl>\n" +
" <aws:Rank>1635</aws:Rank>\n" +
" </aws:TrafficData>\n" +
" </aws:Alexa>\n" +
" </aws:UrlInfoResult>\n" +
" <aws:ResponseStatus xmlns:aws=\"http://alexa.amazonaws.com/doc/2005-10-05/\">\n" +
" <aws:StatusCode>Success</aws:StatusCode>\n" +
" </aws:ResponseStatus>\n" +
" </aws:Response>\n" +
"</aws:UrlInfoResponse>";
DocumentBuilderFactory xmlFact = DocumentBuilderFactory.newInstance();
xmlFact.setNamespaceAware(true);
DocumentBuilder builder = null;
try {
builder = xmlFact.newDocumentBuilder();
} catch (ParserConfigurationException e) {
e.printStackTrace(); }
Document doc = null;
try {
doc = builder.parse(
new ByteArrayInputStream( xmlStr.getBytes()));
} catch (SAXException e) {
e.printStackTrace(); } catch (IOException e) {
e.printStackTrace(); }
System.out.println(doc.getDocumentElement().getNamespaceURI());
System.out.println(xmlFact.isNamespaceAware());
String xpathStr = "//aws:OperationRequest";
XPathFactory xpathFact = XPathFactory.newInstance();
XPath xpath = xpathFact.newXPath();
String result = null;
try {
result = xpath.evaluate(xpathStr, doc);
} catch (XPathExpressionException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
System.out.println("XPath result is \"" + result + "\"");
}
}
import org.w3c.dom.Document;
导入org.xml.sax.SAXException;
导入javax.xml.parsers.DocumentBuilderFactory;
导入javax.xml.parsers.DocumentBuilder;
导入javax.xml.parsers.parserConfiguration异常;
导入javax.xml.xpath.XPathFactory;
导入javax.xml.xpath.xpath;
导入javax.xml.xpath.XPathExpressionException;
导入java.io.ByteArrayInputStream;
导入java.io.IOException;
公开课考试
{
公共静态void main(字符串参数[])
{
字符串xmlStr=“\n”+
“\n”+
“\n”+
“等等\n”+
“\n”+
“\n”+
“\n”+
“\n”+
“harvard.edu/\n”+
“1635\n”+
“\n”+
“\n”+
“\n”+
“\n”+
“成功\n”+
“\n”+
“\n”+
"";
DocumentBuilderFactory xmlFact=DocumentBuilderFactory.newInstance();
setNamespaceAware(true);
DocumentBuilder=null;
试一试{
builder=xmlFact.newDocumentBuilder();
}捕获(ParserConfiguration异常e){
e、 printStackTrace();}
单据单据=空;
试一试{
doc=builder.parse(
新的ByteArrayInputStream(xmlStr.getBytes());
}捕获(SAXE异常){
e、 printStackTrace();}捕获(IOE异常){
e、 printStackTrace();}
System.out.println(doc.getDocumentElement().getNamespaceURI());
System.out.println(xmlFact.isNamespaceAware());
字符串xpathStr=“//aws:OperationRequest”;
XPathFactory xpathFact=XPathFactory.newInstance();
XPath=xpathFact.newXPath();
字符串结果=null;
试一试{
result=xpath.evaluate(xpathStr,doc);
}捕获(XPathExpressionException e){
e、 printStackTrace();//要更改catch语句的主体,请使用文件|设置|文件模板。
}
System.out.println(“XPath结果为\”“+结果+”\”);
}
}
Xpath中的命名空间匹配不仅仅是匹配前缀的字符串。您必须实际定义NamespaceContext对象并将其与xPath关联。实际上,如果文档和xPath中的前缀相同,这一点都不重要
private NamespaceContext ns = new NamespaceContext() {
public String getNamespaceURI(String prefix) {
if (prefix.equals("ns1") return "http://alexa.amazonaws.com/doc/2005-10-05/";
else return XMLConstants.NULL_NS_URI;
}
public String getPrefix(String namespace) {
throw new UnsupportedOperationException();
}
public Iterator getPrefixes(String namespace) {
throw new UnsupportedOperationException();
}};
XPathFactory xpfactory = XPathFactory.newInstance();
XPath xpath = xpfactory.newXPath();
xpath.setNamespaceContext(ns);
String xpathStr = "//ns1:OperationRequest";
//and so on