我想从 .kml 文件中提取值以使用 R 进行描述。
这是文件:
<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2"
xmlns:gx="http://www.google.com/kml/ext/2.2"
xmlns:atom="http://www.w3.org/2005/Atom">
<Document>
<open>1</open>
<visibility>1</visibility>
<name><![CDATA[2013-07-06 4:18pm]]></name>
...
<Placemark>
<name><![CDATA[2013-07-06 4:18pm (Start)]]></name>
<description><![CDATA[]]></description>
<TimeStamp><when>2013-07-06T20:18:56.000Z</when></TimeStamp>
<styleUrl>#start</styleUrl>
<Point>
<coordinates>-78.353348,45.020615,340.29998779296875</coordinates>
</Point>
</Placemark>
<Placemark id="tour">
<name><![CDATA[2013-07-06 4:18pm]]></name>
<description><![CDATA[]]></description>
...
<gx:Track>
<when>2013-07-06T20:18:56.000Z</when>
<gx:coord>-78.353348 45.020615 340.29998779296875</gx:coord>
<when>2013-07-06T20:19:12.000Z</when>
<gx:coord>-78.353315 45.020644 340.29998779296875</gx:coord>
<when>2013-07-06T22:12:23.000Z</when>
<gx:coord>-78.353108 45.020736 342.29998779296875</gx:coord>
<ExtendedData>
...
<Placemark>
<name><![CDATA[2013-07-06 4:18pm (End)]]></name>
<description><![CDATA[Created by Google My Tracks on Android.
Name: 2013-07-06 4:18pm
Activity type: cycling
Description: -
Total distance: 49.62 km (30.8 mi)
Total time: 1:53:28
Moving time: 1:50:17
Average speed: 26.24 km/h (16.3 mi/h)
Average moving speed: 27.00 km/h (16.8 mi/h)
Max speed: 61.20 km/h (38.0 mi/h)
Average pace: 2.29 min/km (3.7 min/mi)
Average moving pace: 2.22 min/km (3.6 min/mi)
Fastest pace: 0.98 min/km (1.6 min/mi)
Max elevation: 406 m (1333 ft)
Min elevation: 265 m (868 ft)
Elevation gain: 690 m (2263 ft)
Max grade: 12 %
Min grade: -11 %
Recorded: 2013-07-06 4:18pm
]]></description>
...
</Placemark>
</Document>
</kml>
这是我要提取的内容,包含在
<description><![CDATA[Created by Google My Tracks on Android.: ]]></description>
IE:
Name: 2013-07-06 4:18pm
Activity type: cycling
Description: -
Total distance: 49.62 km (30.8 mi)
Total time: 1:53:28
Moving time: 1:50:17
Average speed: 26.24 km/h (16.3 mi/h)
Average moving speed: 27.00 km/h (16.8 mi/h)
Max speed: 61.20 km/h (38.0 mi/h)
Average pace: 2.29 min/km (3.7 min/mi)
Average moving pace: 2.22 min/km (3.6 min/mi)
Fastest pace: 0.98 min/km (1.6 min/mi)
Max elevation: 406 m (1333 ft)
Min elevation: 265 m (868 ft)
Elevation gain: 690 m (2263 ft)
Max grade: 12 %
Min grade: -11 %
Recorded: 2013-07-06 4:18p
xmlToList 给了我,我认为 NULL 因为 CDATA 标记意味着解析器不处理以下内容:
xml <- xmlTreeParse("test1.kml", useInternalNodes=TRUE)
xmllist <- xmlToList(xml)
xmllist$Document$Placemark$description
[[1]]
NULL
我认为这就是这意味着“术语 CDATA 用于不应由 XML 解析器解析的文本数据......解析器忽略 CDATA 部分中的所有内容。CDATA 部分以“””开头
以下内容对我也不起作用,可能与 CDATA 相关的原因相同:
z1 <- xpathApply(xml, "//description", xmlValue)
z1
list()
谁能帮我提取文件中的文本?
这是文件的链接:https ://docs.google.com/file/d/0B__iOdFGJbXYOHJGbWJVNW0tS3M/edit?usp=sharing