我得到了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>
我的表达式获取属性值
string(//coverpage/image/@l:href)
android programm中的代码
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:
为什么呢?我做错了什么?
我尝试http://www.freeformatter.com/xpath-tester.html#ad-output进行在线测试 - 一切正常。获取字符串#_0.jpg
答案 0 :(得分:1)
您的问题是您尝试捕获的节点正在使用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;
}
}
(可能重复:How to use XPath on xml docs having default namespace)