使用getnodeset获取节点值

时间:2015-07-30 05:28:00

标签: xml r xpath

我是R的新手,我想用R从网站上获取一些数据。 我试图从Yahoo API获取一些城市索引和城市名称,因此我需要解析XML文件。 但是当我尝试使用getNodeSet()函数获取某些节点的值时,R返回一个空列表。 我们的专家可以就这类问题给我一些建议吗? 非常感谢!

雅虎链接:Yahoo weather API

我已经更新了XML文件。

<query xmlns:yahoo="http://www.yahooapis.com/v1/base.rng" yahoo:count="346" yahoo:created="2015-07-30T02:48:20Z" yahoo:lang="zh-CN">
<diagnostics>
<publiclyCallable>true</publiclyCallable>
<url execution-start-time="24" execution-stop-time="114" execution-time="90">
<![CDATA[
http://wws.geotech.yahooapis.com/v1/counties/CN;start=0;count=1000
]]>
</url>
<user-time>121</user-time>
<service-time>90</service-time>
<build-version>0.2.154</build-version>
</diagnostics>
<results>
<place xmlns="http://where.yahooapis.com/v1/schema.rng" xml:lang="en-US" yahoo:uri="http://where.yahooapis.com/v1/place/26198131">
<woeid>26198131</woeid>
<placeTypeName code="9">Prefecture</placeTypeName>
<name>Wuwei</name>
</place>
<place xmlns="http://where.yahooapis.com/v1/schema.rng" xml:lang="en-US" yahoo:uri="http://where.yahooapis.com/v1/place/26198056">
<woeid>26198056</woeid>
<placeTypeName code="9">Prefecture</placeTypeName>
<name>Jinchang</name>
</place>
<place xmlns="http://where.yahooapis.com/v1/schema.rng" xml:lang="en-US" yahoo:uri="http://where.yahooapis.com/v1/place/26198129">
<woeid>26198129</woeid>
<placeTypeName code="9">Prefecture</placeTypeName>
<name>Lanzhou</name>
</place>
<place xmlns="http://where.yahooapis.com/v1/schema.rng" xml:lang="en-US" yahoo:uri="http://where.yahooapis.com/v1/place/26198130">
<woeid>26198130</woeid>
<placeTypeName code="9">Prefecture</placeTypeName>
<name>Baiyin</name>
</place>
<place xmlns="http://where.yahooapis.com/v1/schema.rng" xml:lang="en-US" yahoo:uri="http://where.yahooapis.com/v1/place/26198128">
<woeid>26198128</woeid>
<placeTypeName code="9">Prefecture</placeTypeName>
<name>Linxia Huizu</name>
</place>
<place xmlns="http://where.yahooapis.com/v1/schema.rng" xml:lang="en-US" yahoo:uri="http://where.yahooapis.com/v1/place/26198133">
<woeid>26198133</woeid>
<placeTypeName code="9">Prefecture</placeTypeName>
<name>Zhangye</name>
</place>
<place xmlns="http://where.yahooapis.com/v1/schema.rng" xml:lang="en-US" yahoo:uri="http://where.yahooapis.com/v1/place/26198127">
<woeid>26198127</woeid>
<placeTypeName code="9">Prefecture</placeTypeName>
<name>Dingxi</name>
</place>
<place xmlns="http://where.yahooapis.com/v1/schema.rng" xml:lang="en-US" yahoo:uri="http://where.yahooapis.com/v1/place/26198125">
<woeid>26198125</woeid>
<placeTypeName code="9">Prefecture</placeTypeName>
<name>Gannan Zangzu</name>
</place>
<place xmlns="http://where.yahooapis.com/v1/schema.rng" xml:lang="en-US" yahoo:uri="http://where.yahooapis.com/v1/place/26198042">
<woeid>26198042</woeid>
<placeTypeName code="9">Prefecture</placeTypeName>
<name>Ili Kazakh</name>
</place>
<place xmlns="http://where.yahooapis.com/v1/schema.rng" xml:lang="en-US" yahoo:uri="http://where.yahooapis.com/v1/place/26198043">
<woeid>26198043</woeid>
<placeTypeName code="9">Prefecture</placeTypeName>
<name>Kizilsu Kirghiz</name>
</place>
<place xmlns="http://where.yahooapis.com/v1/schema.rng" xml:lang="en-US" yahoo:uri="http://where.yahooapis.com/v1/place/26198047">
<woeid>26198047</woeid>
<placeTypeName code="9">Prefecture</placeTypeName>
<name>Aletai</name>
</place>
<place xmlns="http://where.yahooapis.com/v1/schema.rng" xml:lang="en-US" yahoo:uri="http://where.yahooapis.com/v1/place/26198049">
<woeid>26198049</woeid>
<placeTypeName code="9">Prefecture</placeTypeName>
<name>Hetian</name>
</place>
<place xmlns="http://where.yahooapis.com/v1/schema.rng" xml:lang="en-US" yahoo:uri="http://where.yahooapis.com/v1/place/26198262">
<woeid>26198262</woeid>
<placeTypeName code="9">Prefecture</placeTypeName>
<name>Jiamusi</name>
</place>
<place xmlns="http://where.yahooapis.com/v1/schema.rng" xml:lang="en-US" yahoo:uri="http://where.yahooapis.com/v1/place/26198263">
<woeid>26198263</woeid>
<placeTypeName code="9">Prefecture</placeTypeName>
<name>Shuangyashan</name>
</place>
<place xmlns="http://where.yahooapis.com/v1/schema.rng" xml:lang="en-US" yahoo:uri="http://where.yahooapis.com/v1/place/26198057">
<woeid>26198057</woeid>
<placeTypeName code="9">Prefecture</placeTypeName>
<name>Daxing'anling</name>
</place>
</results>
</query>
<!--  total: 121  -->
<!--  pprd1-node1004-lh1.manhattan.ne1.yahoo.com  -->

我试过这段代码:

> library(XML)
> temp = xmlTreeParse("yql.xml",useInternalNodes = TRUE)
> woeid = getNodeSet(temp,"//woeid")
> woeid

但它的回归是:

> list()
attr(,"class")
[1] "XMLNodeSet"

1 个答案:

答案 0 :(得分:1)

woeid继承父place元素的默认命名空间。要使用XPath引用命名空间中的元素,您需要先将前缀映射到相应的命名空间uri,然后在XPath中正确使用该前缀。

我不经常用户,但有些在线消息来源指导我这样的事情:

getNodeSet(temp, "//d:woeid", c(d="http://where.yahooapis.com/v1/schema.rng"))