我最近从这张地图下载了KML文件,并尝试使用该软件包xml2
来提取露营地的信息,例如地理位置,站点周围的设施等; 但我{xml_nodeset (0)}
到最后.
Belows是我用过的代码,
library(xml2) campsites <- read_xml("file_path") xml_find_all(campsites, ".//Placemark")
这是KML文件的结构(您也可以尝试xml_structure(campsites)
),
> library(magrittr) > campsites {xml_document}[1] \n WordPress & MapsMarker.com]] ... > > campsites %>% xml_children %>% xml_children %>% xml_children {xml_nodeset (55)} [1] \n [2]\n \nhttp://www.mountaineering-lohas.org/wp-content/uploads/leaflet-maps-marker-icons/tents.png \n\n [3]\n \nhttp://www.mountaineering-lohas.org/wp-content/uploads/leaflet-maps-marker-icons/tents-1.png \n\n [4]\n \nhttp://www.mountaineering-lohas.org/wp-content/uploads/leaflet-maps-marker-icons/tents1.png \n香港營地 Hong Kong Camp Site [5]\n #tents \næµæ°´éŸ¿ç‡Ÿåœ° ( Lau Shui Heung Camp Site ) \n2013-02-21T04:02:29+08: ... [6] \n #tents \n鶴藪營地(Hok Tau Camp Site) \n2013-02-21T04:02:18+08:00 \n#tents \n涌背營地(Chung Pui Camp Site) \n2013-02-22T11:02:02+08:00 \n#tents \næ±å¹³æ´²ç‡Ÿåœ° (Tung Ping Chau Campsite) \n2013-02-22T11:02:39+08:00 ... [9] \n #tents \nç£ä»”å—營地(Wan Tsai Peninsula South Campsite) \n2013-02-22T11:02:2 ... [10] \n #tents \nç£ä»”西營地 (Wan Tsai Peninsula West Campsite) \n2013-02-22T11:02:3 ... ...
正如您所看到的,有一些名为"Placemark"的节点,为什么我找不到使用的节点xml_find_all
?我的代码中是否有任何错误?
谢谢!
看起来你有一些名称空间.如果将前缀添加到xpath,则可以获取节点集.
xml_ns(campsites) # d1 <-> http://www.opengis.net/kml/2.2 # atom <-> http://www.w3.org/2005/Atom # gx <-> http://www.google.com/kml/ext/2.2 xml_find_all(campsites, ".//d1:Placemark", xml_ns(campsites)) # {xml_nodeset (45)} # [1]\n #tents \n????? ( La ... # [2] \n #tents \n????(Hok T ... # ...
要获取cdata中的文本,您可以使用类似的内容
xml_text(xml_find_all(campsites, "//d1:description", xml_ns(campsites))) # or "//d1:description/text()"