简体   繁体   中英

Subset a list from a XmlNodeList in R

I have a list parsed from xml by using read_xml in R, it produced following structure

I want to subset those inputs into different dataframes assigned as input1, input2, input3 etc .

I tested structure of the list using str()

It produced

 $ node:<externalptr> 
 $ doc :<externalptr> 
 - attr(*, "class")= chr [1:2] "xml_document" "xml_node"

I used df[[1]] , it doesn't seem work in this case.

I don't know how to do it more efficiently. Thanks a lot.

I solved my own issue.

First of all, this is a XmlNodeList as list[[1]] won't work at this case.

A XML file/url needs to be parsed by using xml_df <- read_xml(url, encoding = "UTF-8") first.

Then using library(XML) to load XML

According to https://cran.r-project.org/web/packages/xml2/vignettes/modification.html

I have found that following scripts work in my case:

grandchildren<-xml_children(xml_children(xml_children(xml_children(xml_df)[[1]])[[1]])[8])

Hence,

grandchildren can be used as a normal list.

> grandchildren[[1]]
{xml_node}
<input>
[1] <name>Signal</name>
[2] <id>1062011</id>
[3] <scadaTagID/>
[4] <logInterval xsi:nil="true"/>
[5] <unit/>
[6] <opdata:records>\n  <opdata:record>\n    <value>19</value>\n    <date>2021-05-17T11:14:53</date>\n  </opdata:recor ...

Hope it helps for others who have same issue(s).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM