-->

解析其中R的XML(parse an XML with R)

2019-09-28 02:31发布

我开始在R输入语言的一个项目,我要解析XML,我使用的XML库和功能xmlToDataFrame,XMLPARSE,等等。我想在一个结构化的方式将信息存储在一个数据帧,但我已经遇到问题。 我不能让变量分别采取节点内,每个在其相应的列。 通过使用上述功能,这样可以节省变量的所有的数据在数据帧中的单个行的单个细胞。

我使用的XML如下:

<?xml version="1.0" encoding="UTF-8"?>
-<rest-response>

<type>rest-response</type>

<time-stamp>1392217780000</time-stamp>

<status>OK</status>

<msg-version>1.0.0</msg-version>

<op>inventory</op>


-<response>

<inventorySize>3</inventorySize>

<inventoryMode>SYNCHRONOUS</inventoryMode>

<time>4952</time>


-<items>


-<item>

<epc>00000000000000000000A195</epc>

<ts>1392217779060</ts>

<location-id>adtr</location-id>

<location-pos>0,0,0</location-pos>

<device-id>adtr@1</device-id>

<device-reader>192.168.1.224</device-reader>

<device-readerPort>1</device-readerPort>

<device-readerMuxPort>0</device-readerMuxPort>

<device-readerMuxPort2>0</device-readerMuxPort2>

<tag-rssi>-49.0</tag-rssi>

<tag-readcount>36.0</tag-readcount>

<tag-phase>168.0</tag-phase>

</item>


-<item>

<epc>00000000000000000000A263</epc>

<ts>1392217779065</ts>

<location-id>adtr</location-id>

<location-pos>0,0,0</location-pos>

<device-id>adtr@1</device-id>

<device-reader>192.168.1.224</device-reader>

<device-readerPort>1</device-readerPort>

<device-readerMuxPort>0</device-readerMuxPort>

<device-readerMuxPort2>0</device-readerMuxPort2>

<tag-rssi>-49.0</tag-rssi>

<tag-readcount>36.0</tag-readcount>

<tag-phase>0.0</tag-phase>

</item>


-<item>

<epc>B00000000000001101080802</epc>

<ts>1392217779323</ts>

<location-id>adtr</location-id>

<location-pos>0,0,0</location-pos>

<device-id>adtr@1</device-id>

<device-reader>192.168.1.224</device-reader>

<device-readerPort>1</device-readerPort>

<device-readerMuxPort>0</device-readerMuxPort>

<device-readerMuxPort2>0</device-readerMuxPort2>

<tag-rssi>-72.0</tag-rssi>

<tag-readcount>27.0</tag-readcount>

<tag-phase>157.0</tag-phase>

</item>

</items>

</response>

</rest-response>

一切都在里面项获得它作为一个单一的价值,我想通过不同的概念把四分五裂。

另外重要的一点是,XML可能会改变,但它的结构将始终是相同的,但有可能是多个项目

任何想法?

Answer 1:

所以,我认为想要在<items>中的数据帧。 假设你的XML是在变量xml.text ,这将工作:

library(XML)
xml   <- xmlInternalTreeParse(xml.text)  # assumes your xml in variable xml.text
items <- getNodeSet(xml,"//items/item")
df    <- xmlToDataFrame(items)
df
#                        epc            ts location-id location-pos device-id device-reader device-readerPort device-readerMuxPort device-readerMuxPort2 tag-rssi tag-readcount tag-phase
# 1 00000000000000000000A195 1392217779060        adtr        0,0,0    adtr@1 192.168.1.224                 1                    0                     0    -49.0          36.0     168.0
# 2 00000000000000000000A263 1392217779065        adtr        0,0,0    adtr@1 192.168.1.224                 1                    0                     0    -49.0          36.0       0.0
# 3 B00000000000001101080802 1392217779323        adtr        0,0,0    adtr@1 192.168.1.224                 1                    0                     0    -72.0          27.0     157.0

我还以为你在浏览器和剪切/粘贴(这可以解释所显示的该XML -<tag> )。 否则,不能很好地形成你的XML。



文章来源: parse an XML with R