Use getnodeset to get node value

I am new to R and I want to use R to get some data from the site. I tried to get the names of cities and towns from the Yahoo API and so I need to parse the XML file. but when I tried to get the value of some nodes with a function getNodeSet()

, R returns an empty list. Can our experts give me some advice on this issue? Thank you so much!

Yahoo link: Yahoo weather API

and I have updated the XML file.

<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  -->

      

I tried this code:

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

      

But his return:

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

      

+3


source to share


1 answer


woeid

inherits the default namespace of the parent element place

. To refer to an element in a namespace using XPath, you need to match a prefix to first point to the uri of the corresponding namespace, then use that prefix correctly for your XPath.

I don't see r often, but some online sources lead me to something like this:



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

      

+1


source







All Articles