Java 命名空间声明的XPath解析
我正在使用XPath提取URL128 XML元素的值。虽然在下面的例子中我只有一个,但这些可能有很多。当我包括xmlns时http://c1.net.corbis.com/'在SearchResponse元素上,我得到了一个空节点列表,但当我删除该名称空间元素时,它工作正常。是否有我缺少的配置Java 命名空间声明的XPath解析,java,xpath,Java,Xpath,我正在使用XPath提取URL128 XML元素的值。虽然在下面的例子中我只有一个,但这些可能有很多。当我包括xmlns时http://c1.net.corbis.com/'在SearchResponse元素上,我得到了一个空节点列表,但当我删除该名称空间元素时,它工作正常。是否有我缺少的配置 String xmlData = "<SearchResponse xmlns='http://c1.net.corbis.com/'><searchResultDataXML>&
String xmlData = "<SearchResponse xmlns='http://c1.net.corbis.com/'><searchResultDataXML><SearchResultData><SearchRequestUID Scope='Public' Type='Guid' Value='{cded773c-c4b7-4dd8-aaee-8e5b8b7a2475}'/><StartPosition Scope='Public' Type='Long' Value='1'/><EndPosition Scope='Public' Type='Long' Value='50'/><TotalHits Scope='Public' Type='Long' Value='323636'/></SearchResultData></searchResultDataXML><imagesXML><Images><Image><ImageUID Scope='Public' Type='Guid' Value='{a6f6d3e2-2c3f-4502-9741-eae2e1bb573a}'/><CorbisID Scope='Public' Type='String' Value='42-25763849'/><Title Scope='Public' Type='String' Value='Animals figurines'/><CreditLine Scope='Public' Type='String' Value='© Ocean/Corbis'/><IsRoyaltyFree Scope='Public' Type='Boolean' Value='True'/><AspectRatio Scope='Public' Type='String' Value='0.666667'/><URL128 Scope='Public' Type='String' Value='http://cachens.corbis.com/CorbisImage/thumb/25/76/38/25763849/42-25763849.jpg'/></Image></Images></imagesXML></SearchResponse>";
InputSource source = new InputSource(new StringReader(xmlData));
XPath xPath = XPathFactory.newInstance().newXPath();
NodeList list = null;
try {
list = (NodeList) xPath.evaluate("//URL128/@Value", source, XPathConstants.NODESET);
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
for (int i = 0; i < list.getLength(); i++) {
System.out.println(list.item(i).getTextContent());
}
String xmlData=”“;
InputSource source=新的InputSource(新的StringReader(xmlData));
XPath=XPathFactory.newInstance().newXPath();
节点列表=null;
试一试{
list=(NodeList)xPath.evaluate(“//URL128/@Value”,源代码,XPathConstants.NODESET);
}捕获(例外情况除外){
System.out.println(例如getMessage());
}
对于(int i=0;i
长话短说,您需要为您的XPath提供一个
final XPath xPath = XPathFactory.newInstance().newXPath();
xPath.setNamespaceContext(new NamespaceContext() {
@Override
public Iterator<String> getPrefixes(final String namespaceURI) {
return null;
}
@Override
public String getPrefix(final String namespaceURI) {
return null;
}
@Override
public String getNamespaceURI(final String prefix) {
return "http://c1.net.corbis.com/";
}
});
final NodeList list = (NodeList) xPath.evaluate("//c:URL128/@Value", source, XPathConstants.NODESET);
for (int i = 0; i < list.getLength(); i++) {
System.out.println(list.item(i).getTextContent());
}
final XPath=XPathFactory.newInstance().newXPath();
setNamespaceContext(新的NamespaceContext(){
@凌驾
公共迭代器getPrefixes(最终字符串namespaceURI){
返回null;
}
@凌驾
公共字符串getPrefix(最终字符串namespaceURI){
返回null;
}
@凌驾
公共字符串getNamespaceURI(最终字符串前缀){
返回“http://c1.net.corbis.com/";
}
});
最终节点列表=(NodeList)xPath.evaluate(“//c:URL128/@Value”,源代码,XPathConstants.NODESET);
对于(int i=0;i
在这种情况下,XPath似乎需要我们实现的唯一方法是getNamespaceURI(字符串前缀)
请注意,“c:URL128”中的实际前缀实际上并不重要,在这种情况下,您可以轻松地使用“:URL128”。如果XML中有多个名称空间,那么区分它们就变得很重要(使用映射
或一系列if-then-else
if相对较少的元素)
如果您不能或不想硬编码前缀,您可以自己从XML文档中提取前缀,但这需要更多的代码
有关更多详细信息,请参见。以下是实现AlistairIsreal概述的两种方法:
如果使用spring,您可以依赖org.springframework.util.xml.SimpleNamespaceContext接口
InputSource source = new InputSource(new StringReader(unescaped));
XPath xPath = XPathFactory.newInstance().newXPath();
NodeList list = null;
try
{
SimpleNamespaceContext nsCtx = new SimpleNamespaceContext();
nsCtx.bindNamespaceUri("ns", "http://c1.net.corbis.com/");
xPath.setNamespaceContext(nsCtx);
list = (NodeList) xPath.evaluate("//ns:URL128/@Value", source, XPathConstants.NODESET);
} catch (Exception ex)
{
System.out.println(ex.getMessage());
}
for (int i = 0; i < list.getLength(); i++)
{
System.out.println(list.item(i).getTextContent());
}
InputSource=newinputsource(newstringreader(unscaped));
XPath=XPathFactory.newInstance().newXPath();
节点列表=null;
尝试
{
SimpleNamespaceContext nsCtx=新的SimpleNamespaceContext();
nsCtx.bindNamespaceUri(“ns”http://c1.net.corbis.com/");
setNamespaceContext(nsCtx);
list=(NodeList)xPath.evaluate(“//ns:URL128/@Value”,源代码,XPathConstants.NODESET);
}捕获(例外情况除外)
{
System.out.println(例如getMessage());
}
对于(int i=0;i
有一个稍微简单的解决方案,它不需要在代码中放置硬编码的URI引用。。。只需解析名称空间感知属性设置为false的文档
String xmlData = "<SearchResponse xmlns='http://c1.net.corbis.com/'><searchResultDataXML><SearchResultData><SearchRequestUID Scope='Public' Type='Guid' Value='{cded773c-c4b7-4dd8-aaee-8e5b8b7a2475}'/><StartPosition Scope='Public' Type='Long' Value='1'/><EndPosition Scope='Public' Type='Long' Value='50'/><TotalHits Scope='Public' Type='Long' Value='323636'/></SearchResultData></searchResultDataXML><imagesXML><Images><Image><ImageUID Scope='Public' Type='Guid' Value='{a6f6d3e2-2c3f-4502-9741-eae2e1bb573a}'/><CorbisID Scope='Public' Type='String' Value='42-25763849'/><Title Scope='Public' Type='String' Value='Animals figurines'/><CreditLine Scope='Public' Type='String' Value='© Ocean/Corbis'/><IsRoyaltyFree Scope='Public' Type='Boolean' Value='True'/><AspectRatio Scope='Public' Type='String' Value='0.666667'/><URL128 Scope='Public' Type='String' Value='http://cachens.corbis.com/CorbisImage/thumb/25/76/38/25763849/42-25763849.jpg'/></Image></Images></imagesXML></SearchResponse>";
InputSource source = new InputSource(new StringReader(xmlData));
// create doc instance instead of passing source straight to XPath...
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(false); // must be false
DocumentBuilder builder = factory.newDocumentBuilder();
final Document doc = builder.parse(source);
XPath xPath = XPathFactory.newInstance().newXPath();
// use doc instead
NodeList list = (NodeList) xPath.evaluate("//URL128/@Value", doc,
XPathConstants.NODESET);
for (int i = 0; i < list.getLength(); i++) {
System.out.println(list.item(i).getTextContent());
}
String xmlData=”“;
InputSource source=新的InputSource(新的StringReader(xmlData));
//创建文档实例,而不是直接将源代码传递给XPath。。。
DocumentBuilderFactory工厂=DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(false);//一定是假的
DocumentBuilder=factory.newDocumentBuilder();
最终文档doc=builder.parse(来源);
XPath=XPathFactory.newInstance().newXPath();
//改用doc
NodeList list=(NodeList)xPath.evaluate(“//URL128/@Value”,doc,
XPathConstants.NODESET);
对于(int i=0;i