Android为什么xPath.String返回空字符串?

Android为什么xPath.String返回空字符串?,android,xml,xpath,Android,Xml,Xpath,我得到了xml <FictionBook xmlns="http://www.gribuser.ru/xml/fictionbook/2.0" xmlns:l="http://www.w3.org/1999/xlink"> <description> <title-info> <genre>love_contemporary</genre> <author>

我得到了xml

<FictionBook xmlns="http://www.gribuser.ru/xml/fictionbook/2.0" xmlns:l="http://www.w3.org/1999/xlink">  
  <description> 
    <title-info> 
      <genre>love_contemporary</genre>  
      <author> 
        <first-name>Sylvain</first-name>  
        <last-name>Reynard</last-name> 
      </author>  
      <book-title>Gabriel's Inferno</book-title>  
      <annotation> 
        <p>Enigmatic and sexy, Professor Gabriel Emerson is a well respected Dante specialist by day, but by night he devotes himself to an uninhibited life of pleasure. He uses his notorious good looks and sophisticated charm to gratify his every whim, but is secretly tortured by his dark past and consumed by the profound belief that he is beyond all hope of redemption. When the sweet and innocent Julia Mitchell enrolls as his graduate student, his attraction and mysterious connection to her not only jeopardizes his career, but sends him on a journey in which his past and his present collide. An intriguing and sinful exploration of seduction, forbidden love and redemption, Gabriel's Inferno is a captivating and wildly passionate tale of one man's escape from his own personal hell as he tries to earn the impossible…forgiveness and love.</p> 
      </annotation>  
      <date/>  
      <coverpage> 
        <image l:href="#_0.jpg"/> 
      </coverpage>  
      <lang>en</lang>  
      <src-lang>en</src-lang>  
      <sequence name="Gabriel's Inferno" number="1"/> 
    </title-info>  
    <document-info> 
      <author> 
        <first-name/>  
        <last-name/> 
      </author>  
      <date/>  
      <id>2aec7273-a8a4-4edc-803a-820c4d76bc3f</id>  
      <version>1.0</version> 
    </document-info>  
    <publish-info> 
      <book-name>Gabriel's Inferno</book-name>  
      <year>2011</year> 
    </publish-info> 
  </description> 
</FictionBook>
android程序中的代码

XPathFactory factory = XPathFactory.newInstance();
XPath xpath = factory.newXPath();
String expression;
String attrValue;

expression = "string(//coverpage/image/@l:href)";
try {
    attrValue =  xpath.compile(expression).evaluate(obj,
        XPathConstants.STRING).toString();
    System.out.println("VAL XML:"+attrValue);

} catch (XPathExpressionException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
但在控制台上,我只得到:

VAL XML:
为什么??我做错了什么


我尝试在线测试——一切正常。获取字符串#_0.jpg

您的问题是,您试图捕获的节点正在使用XML名称空间,而工厂没有意识到这一点。我认为有两种解决方案:

不定义名称空间

使用
local-name()

//*[local-name() = 'coverpage']/*[local-name() = 'image']/@*[local-name() = 'href']
//coverpage/image/@*[local-name()='href']
也可以使用)

定义命名空间

让XPathFactory知道不同的名称空间,以便它知道使用哪个名称空间

import javax.xml.namespace.NamespaceContext;
...
xpath.setNamespaceContext(new MyNamespaceContext());
attrValue =  xpath.compile(expression).evaluate(obj,
                    XPathConstants.STRING).toString();
...
private static class MyNamespaceContext implements NamespaceContext {

    public String getNamespaceURI(String prefix) {
        if("l".equals(prefix)) {
            return "http://www.w3.org/1999/xlink";
        }
        return null;
    }

    public String getPrefix(String namespaceURI) {
        return null;
    }

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

}

(可能重复:)

这很有效,谢谢。但我不明白如何编写xpath来获取属性值而不使用名称空间?我尝试了//*[local-name()='coverpage']/*[local-name()='image']/@l:href-不起作用:(这是因为你应该对
href
属性使用
local-name()='coverpage']/*[local-name()='coverpage']/*[local-name()='image']/*[local-name()='image']/*[local-name()='href']
(这可能也适用于
//coverpage/image/*[local-name()='href']
)。我将更新答案。
import javax.xml.namespace.NamespaceContext;
...
xpath.setNamespaceContext(new MyNamespaceContext());
attrValue =  xpath.compile(expression).evaluate(obj,
                    XPathConstants.STRING).toString();
...
private static class MyNamespaceContext implements NamespaceContext {

    public String getNamespaceURI(String prefix) {
        if("l".equals(prefix)) {
            return "http://www.w3.org/1999/xlink";
        }
        return null;
    }

    public String getPrefix(String namespaceURI) {
        return null;
    }

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

}