我有以下xml:
<?xml version="1.0" encoding="UTF-8"?>
<GSP VER="3.2">
<TM>0</TM>
<Q>monkey pay</Q>
<Context>
<total_results>3</total_results>
</Context>
<RES EN="3" SN="1">
<M>3</M>
<R N="1">
<U>link1</U>
<T>title1</T>
<S>snippet1</S>
</R>
<R N="2">
<U>link2</U>
<T>title2</T>
<S>snippet2</S>
<PageMap>
<DataObject type="metatags">
<Attribute name="viewport" value="width=992, maximum-scale=2.0" />
<Attribute name="dc.subject" value="atributevalue" />
<Attribute name="og:title" value="atributevalue" />
<Attribute name="og:description" value="atributevalue" />
<Attribute name="og:image" value="atributevalue" />
</DataObject>
<DataObject type="cse_image">
<Attribute name="src" value="atributevalue" />
</DataObject>
<DataObject type="cse_thumbnail">
<Attribute name="width" value="atributevalue" />
<Attribute name="height" value="atributevalue" />
<Attribute name="src" value="image2" />
</DataObject>
</PageMap>
</R>
<R N="3">
<U>link3</U>
<T>title3</T>
<S>snippet3</S>
</R>
</RES>
</GSP>
我需要获取属性名称的值=&#34; src&#34;标签&#34;属性&#34;在&#34; DataObject&#34;内部标签,其属性类型为&#34; cse_thumbnail&#34;。
这个的xpath是: / GSP / RES / R [@ N = 2] /页映射/数据对象[@type =&#39; cse_thumbnail&#39;] /属性[@name =&#39; SRC&#39;] / @值
您知道如何在Java中使用DOMXpath提取它吗?
现在我可以使用链接,标题和片段获取所有R标记,但我无法提取PageMap值。
这就是我解析R标签的方法:
List resultNodes = new DOMXPath("/GSP/RES/R").selectNodes(domDoc);
Iterator nodeIter = resultNodes.iterator();
while (nodeIter.hasNext()) {
Object obj = nodeIter.next();
Element elem = (Element) object;
}
答案 0 :(得分:0)
我不知道它为什么不起作用。 标准的java实现怎么样:
XPathFactory xPathfactory = XPathFactory.newInstance();
XPath xpath = xPathfactory.newXPath();
XPathExpression expr = xpath.compile("your path");