java中的xpath问题 [英] Issues with xpath in java

查看:111
本文介绍了java中的xpath问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前在java中的xpath表达式存在问题。
我试图获得一个shopNames的列表!



我得到以下XML;

 <?xml version =1.0encoding =UTF-8?> 
< w:shops xmlns:w =namespace>
< w:shop>
< w:shopID> 1< / w:shopID>
< w:shopName> ShopName< / w:shopName>
< w:shopURL> ShopUrl< / w:shopURL>
< / w:shop>
< w:shop>
< w:shopID> 2< / w:shopID>
< w:shopName> ShopNames< / w:shopName>
< w:shopURL> ShopUrl< / w:shopURL>
< / w:shop>
< / w:shops>

我正在一个文档中给这个函数一样的功能:

 列表< String> getShops(文件d)
抛出异常
{
列表< String> shopnames = new ArrayList< String>();

XPath xpath = XPathFactory.newInstance()。newXPath();

XPathExpression expr = xpath.compile(/ descendant :: w:shop / descendant :: w:shopName);
NodeList nodes =(NodeList)expr.evaluate(d,XPathConstants.NODESET);

for(int x = 0; x< nodes.getLength(); x ++)
{
shopnames.add(+ nodes.item(x).getNodeValue ));
}
返回商店;
}

但问题是它只返回一个空列表,我怀疑它是我的xpath表达式,但我不确定它。



任何人都看到这里的问题?

解决方案

根元素不是商店,而是商店。我想,你必须编译这个表达式:

  xpath.compile(/ descendant :: w:shops / descendant ::女:店/后裔::宽:SHOPNAME); 






您可能需要设置一个命名空间上下文: / p>

  xpath.setNamespaceContext(new NamespaceContext(){

public String getNamespaceURI(String prefix){
if(prefix.equals(w))returnnamespace;
else return XMLConstants.NULL_NS_URI;
}

public String getPrefix(String namespace){
if(namespace.equals(namespace))returnw;
else return null;
}

public Iterator getPrefixes(String namespace){return null ;}

});

并进行解析,以便文档知道命名空间

  DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 
dbf.setNamespaceAware(true); //< ----
DocumentBuilder db = dbf.newDocumentBuilder();
文档xmlDom = db.parse(./ stores.xml);


I'm currently having an issue with my xpath expressions in java. I'm trying to get a list of shopNames!

I got the following XML;

<?xml version="1.0" encoding="UTF-8"?>
<w:shops xmlns:w="namespace">
    <w:shop>
        <w:shopID>1</w:shopID>
        <w:shopName>ShopName</w:shopName>
        <w:shopURL>ShopUrl</w:shopURL>
    </w:shop>
    <w:shop>
        <w:shopID>2</w:shopID>
        <w:shopName>ShopNames</w:shopName>
        <w:shopURL>ShopUrl</w:shopURL>
    </w:shop>
</w:shops>

And I'm feeding this in a Document to a function alike this:

List<String> getShops(Document d)
    throws Exception
{
    List<String> shopnames = new ArrayList<String>();

    XPath xpath = XPathFactory.newInstance().newXPath();

    XPathExpression expr = xpath.compile("/descendant::w:shop/descendant::w:shopName");
    NodeList nodes = (NodeList) expr.evaluate(d, XPathConstants.NODESET);

    for(int x=0; x<nodes.getLength(); x++)
    {
        shopnames.add("" + nodes.item(x).getNodeValue());
    }
    return shopnames;
}

However the issue is that it simply returns an empty list, I'm suspecting it to be my xpath expression, but I'm not sure about it.

Anyone see the issue here?

解决方案

The root Element is not shop but shops. I think, you have to compile this expression:

xpath.compile("/descendant::w:shops/descendant::w:shop/descendant::w:shopName");


You may have to set a namespace context:

xpath.setNamespaceContext(new NamespaceContext() {

   public String getNamespaceURI(String prefix) {
    if (prefix.equals("w")) return "namespace";
    else return XMLConstants.NULL_NS_URI;
   }

   public String getPrefix(String namespace) {
    if (namespace.equals("namespace")) return "w";
    else return null;
   }

   public Iterator getPrefixes(String namespace) {return null;}

});

and parse so that the document is aware of namespaces

DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);  // <----
DocumentBuilder db = dbf.newDocumentBuilder();
Document xmlDom = db.parse("./shops.xml");

这篇关于java中的xpath问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆