我正在尝试将以下XML文件转换为数据框,数据时间也作为数据框的列,但我无法提取日期时间属性。关于如何在R中做到这一点的任何想法?使用示例代码的演示将有助于理解如何在R中执行此操作。
<?xml version="1.0" encoding="UTF-8"?>
<Group snapshotTime="2018-05-30T19:33:44.352Z">
<Links>
<rel>self</rel>
<href>https:cloud.com/Group/1</href>
</Links>
<Links>
<rel>last</rel>
<href>https:cloud.com/Group/6</href>
</Links>
<Links>
<rel>next</rel>
<href>https:cloud.com/Group/2</href>
</Links>
<Equipment>
<EquipmentHeader>
<Name>CASE IH</Name>
<Model>1100</Model>
<EquipmentID> Desk</EquipmentID>
<SerialNumber>1231</SerialNumber>
<PIN>123</PIN>
</EquipmentHeader>
<Location datetime="2012-06-25T11:14:54.000Z">
<Latitude>12.573722</Latitude>
<Longitude>-45.515805</Longitude>
</Location>
<Ophrs datetime="2012-03-01T17:42:37.000Z">
<Hour>1968.80</Hour>
</Ophrs>
</Equipment>
<Equipment>
<EquipmentHeader>
<Name>CALL</Name>
<Model>L2048</Model>
<EquipmentID>1MM772GP4</EquipmentID>
<SerialNumber>1TT772GPVJF688214</SerialNumber>
<PIN>1TT772G4</PIN>
</EquipmentHeader>
<Location datetime="2018-05-30T19:22:46.000Z">
<Latitude>15.518556</Latitude>
<Longitude>-55.422444</Longitude>
</Location>
<CumulativeIdleHours datetime="2018-05-30T19:02:46.000Z">
<Hour>14.74</Hour>
</CumulativeIdleHours>
<Ophrs datetime="2018-05-30T19:22:48.000Z">
<Hour>52.35</Hour>
</Ophrs>
<Distance datetime="2018-05-30T19:02:46.000Z">
<OdometerUnits>kilometre</OdometerUnits>
<Odometer>130.9</Odometer>
</Distance>
<FuelUsed datetime="2018-05-30T19:02:46.000Z">
<FuelUnits>litre</FuelUnits>
<FuelConsumed>395</FuelConsumed>
</FuelUsed>
</Equipment>
</Group>
答案 0 :(得分:0)
以下是获取datetime
节点的Location
属性的方法:
library("xml2")
library("tidyverse")
temp <- '<?xml version="1.0" encoding="UTF-8"?>
<Group snapshotTime="2018-05-30T19:33:44.352Z">
<Links>
<rel>self</rel>
<href>https:cloud.com/Group/1</href>
</Links>
<Links>
<rel>last</rel>
<href>https:cloud.com/Group/6</href>
</Links>
<Links>
<rel>next</rel>
<href>https:cloud.com/Group/2</href>
</Links>
<Equipment>
<EquipmentHeader>
<Name>CASE IH</Name>
<Model>1100</Model>
<EquipmentID> Desk</EquipmentID>
<SerialNumber>1231</SerialNumber>
<PIN>123</PIN>
</EquipmentHeader>
<Location datetime="2012-06-25T11:14:54.000Z">
<Latitude>12.573722</Latitude>
<Longitude>-45.515805</Longitude>
</Location>
<Ophrs datetime="2012-03-01T17:42:37.000Z">
<Hour>1968.80</Hour>
</Ophrs>
</Equipment>
<Equipment>
<EquipmentHeader>
<Name>CALL</Name>
<Model>L2048</Model>
<EquipmentID>1MM772GP4</EquipmentID>
<SerialNumber>1TT772GPVJF688214</SerialNumber>
<PIN>1TT772G4</PIN>
</EquipmentHeader>
<Location datetime="2018-05-30T19:22:46.000Z">
<Latitude>15.518556</Latitude>
<Longitude>-55.422444</Longitude>
</Location>
<CumulativeIdleHours datetime="2018-05-30T19:02:46.000Z">
<Hour>14.74</Hour>
</CumulativeIdleHours>
<Ophrs datetime="2018-05-30T19:22:48.000Z">
<Hour>52.35</Hour>
</Ophrs>
<Distance datetime="2018-05-30T19:02:46.000Z">
<OdometerUnits>kilometre</OdometerUnits>
<Odometer>130.9</Odometer>
</Distance>
<FuelUsed datetime="2018-05-30T19:02:46.000Z">
<FuelUnits>litre</FuelUnits>
<FuelConsumed>395</FuelConsumed>
</FuelUsed>
</Equipment>
</Group>'
temp %>% xml2::read_xml() %>% rvest::xml_nodes("Location") %>% xml2::xml_attr("datetime")
希望这会有所帮助。