我正在尝试使用XML包将GPS运行数据从TCX文件导入R.这是我的数据的一小部分样本(只有3个跟踪点,而不是~900)
<?xml version="1.0" encoding="UTF-8"?>
<TrainingCenterDatabase xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.garmin.com/xmlschemas/TrainingCenterDatabase/v2">
<Activities>
<Activity Sport="Running">
<Id>2011-10-30T16:05:48Z</Id>
<Lap StartTime="2011-10-30T16:05:48Z">
<TotalTimeSeconds>3855.99</TotalTimeSeconds>
<DistanceMeters>12498.8115</DistanceMeters>
<MaximumSpeed>4.45662498</MaximumSpeed>
<Calories>1011</Calories>
<Intensity>Active</Intensity>
<TriggerMethod>Manual</TriggerMethod>
<Track>
<Trackpoint>
<Time>2011-10-30T16:05:48Z</Time>
<Position>
<LatitudeDegrees>52.33613318</LatitudeDegrees>
<LongitudeDegrees>-1.58814317</LongitudeDegrees>
</Position>
<AltitudeMeters>77.5234375</AltitudeMeters>
<DistanceMeters>0.00000000</DistanceMeters>
</Trackpoint>
<Trackpoint>
<Time>2011-10-30T16:05:49Z</Time>
<Position>
<LatitudeDegrees>52.33614810</LatitudeDegrees>
<LongitudeDegrees>-1.58814283</LongitudeDegrees>
</Position>
<AltitudeMeters>77.5234375</AltitudeMeters>
<DistanceMeters>1.77584004</DistanceMeters>
</Trackpoint>
<Trackpoint>
<Time>2011-10-30T16:05:54Z</Time>
<Position>
<LatitudeDegrees>52.33627098</LatitudeDegrees>
<LongitudeDegrees>-1.58818323</LongitudeDegrees>
</Position>
<AltitudeMeters>76.0814209</AltitudeMeters>
<DistanceMeters>15.7694969</DistanceMeters>
</Trackpoint>
</Track>
</Lap>
</Activity>
</Activities>
</TrainingCenterDatabase>
我正在尝试使用
读取跟踪点doc = xmlParse("filetest.tcx")
xmlToDataFrame(nodes = getNodeSet(doc, "//Trackpoint"))
但是这会失败,结果是一个空的数据框。但是我发现如果我删除
xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.garmin.com/xmlschemas/TrainingCenterDatabase/v2"
来自文件开头的TrainingCenterDatabase标记,此导入按预期正确运行。 IE浏览器。使用以下数据:
<?xml version="1.0" encoding="UTF-8"?>
<TrainingCenterDatabase>
<Activities>
<Activity Sport="Running">
<Id>2011-10-30T16:05:48Z</Id>
<Lap StartTime="2011-10-30T16:05:48Z">
<TotalTimeSeconds>3855.99</TotalTimeSeconds>
<DistanceMeters>12498.8115</DistanceMeters>
<MaximumSpeed>4.45662498</MaximumSpeed>
<Calories>1011</Calories>
<Intensity>Active</Intensity>
<TriggerMethod>Manual</TriggerMethod>
<Track>
<Trackpoint>
<Time>2011-10-30T16:05:48Z</Time>
<Position>
<LatitudeDegrees>52.33613318</LatitudeDegrees>
<LongitudeDegrees>-1.58814317</LongitudeDegrees>
</Position>
<AltitudeMeters>77.5234375</AltitudeMeters>
<DistanceMeters>0.00000000</DistanceMeters>
</Trackpoint>
<Trackpoint>
<Time>2011-10-30T16:05:49Z</Time>
<Position>
<LatitudeDegrees>52.33614810</LatitudeDegrees>
<LongitudeDegrees>-1.58814283</LongitudeDegrees>
</Position>
<AltitudeMeters>77.5234375</AltitudeMeters>
<DistanceMeters>1.77584004</DistanceMeters>
</Trackpoint>
<Trackpoint>
<Time>2011-10-30T16:05:54Z</Time>
<Position>
<LatitudeDegrees>52.33627098</LatitudeDegrees>
<LongitudeDegrees>-1.58818323</LongitudeDegrees>
</Position>
<AltitudeMeters>76.0814209</AltitudeMeters>
<DistanceMeters>15.7694969</DistanceMeters>
</Trackpoint>
</Track>
</Lap>
</Activity>
</Activities>
</TrainingCenterDatabase>
我得到了我想要的数据帧:(除了位置没有分成lat和long,但我预计我应该能够处理它,除非有人可以建议一种更简单的方法直接使用XPath ?)
> xmlToDataFrame(nodes = getNodeSet(doc, "//Trackpoint"))
Time Position AltitudeMeters DistanceMeters
1 2011-10-30T16:05:48Z 52.33613318-1.58814317 77.5234375 0.00000000
2 2011-10-30T16:05:49Z 52.33614810-1.58814283 77.5234375 1.77584004
3 2011-10-30T16:05:54Z 52.33627098-1.58818323 76.0814209 15.7694969
显然,我不想手动从我要导入的任何文件中删除它。有没有我做错了(或许有XPath?)这会阻止它工作,或者是否有解决方法从XML数据中删除该部分?
非常感谢
答案 0 :(得分:10)
这是命名空间问题。就这样做
xmlToDataFrame(nodes <- getNodeSet(doc, "//ns:Trackpoint", "ns"))
要直接获得按纬度和经度划分的位置,您可以执行以下操作
nodes <- getNodeSet(doc, "//ns:Trackpoint", "ns")
mydf <- plyr::ldply(nodes, as.data.frame(xmlToList))
setNames(mydf, c('time', 'lat', 'long', 'alt', 'distance'))
它给出了
time lat long alt distance
1 2011-10-30T16:05:48Z 52.33613318 -1.58814317 77.5234375 0.00000000
2 2011-10-30T16:05:49Z 52.33614810 -1.58814283 77.5234375 1.77584004
3 2011-10-30T16:05:54Z 52.33627098 -1.58818323 76.0814209 15.7694969