【发布时间】:2014-02-12 18:48:49
【问题描述】:
我正在用 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>
item 中的所有内容都将其作为单个值,我想用不同的概念来分解。
另一个重要的一点是XML可能会改变,但它的结构总是一样的,但可能会有更多的项目
有什么想法吗?
【问题讨论】:
-
请分享您已经尝试过的代码。