2016-09-21 65 views
1

我從Google地球專業版導出了kml文件,包含37個文件夾,每個文件夾包含「次要」文件夾數量,共計「次要」文件夾數量爲168個,每個「未成年人「有3個地標。在KML文件中添加標籤使用R

我製作了HTML代碼,我用R做了它,並且想要將這個kml文件導入到R中,並將這個HTML代碼放到每個「次要」文件夾的第一個「地標」中,這個HTML代碼不是常量,它有像這個代碼中的表中的值這樣的變量,並且這個變量將從我爲這個「次要」文件夾製作的數據框中附加,當我編輯這個HTML代碼時,我會將它放入第一個「地標」 「次要」文件夾,等等其他「次要」文件夾。

R中是否有任何功能可以將此代碼添加到kml文件中?

這裏的 「說明」 中R.代碼

URL <- paste("file:///C:/Users/pc/Downloads/Googletraffic/Tazbet/Autostrad;Helwan To Da2ery/",FileName,sep = "") 
library(XML) 

top = newXMLNode("description") 

table = newXMLNode("table ", attrs = c(width = 300, border = 1), parent = top) 
tbody <- newXMLNode("tbody",parent = tr) 
tr <- newXMLNode("tr",parent = table) 
th <- newXMLNode("th",attrs = c(scope = "col"),scope1 = max(Bey2ollak$V3),parent = tr) 
th <- newXMLNode("th",attrs = c(scope = "col"),scope1 = "MD",parent = tr) 
th <- newXMLNode("th",attrs = c(scope = "col"),scope1 = "PM",parent = tr) 
tr <- newXMLNode("tr",parent = table) 
th <- newXMLNode("th",attrs = c(scope = "col"),scope1 = max(Bey2ollak$V3),parent = tr) 
th <- newXMLNode("th",attrs = c(scope = "col"),scope1 = "MD",parent = tr) 
th <- newXMLNode("th",attrs = c(scope = "col"),scope1 = "PM",parent = tr) 
tr <- newXMLNode("tr",parent = table) 
th <- newXMLNode("th",attrs = c(scope = "col"),scope1 = max(Bey2ollak$V3),parent = tr) 
th <- newXMLNode("th",attrs = c(scope = "col"),scope1 = "MD",parent = tr) 
th <- newXMLNode("th",attrs = c(scope = "col"),scope1 = "PM",parent = tr) 

th <- newXMLNode("img",attrs = c(src = URL,width = "700",height= "777",alt=""),parent =top) 



top 

這裏是控制檯輸出

<description> 
    <table width="300" border="1"> 
    <tr> 
     <th scope="col">5</th> 
     <th scope="col">MD</th> 
     <th scope="col">PM</th> 
    </tr> 
    <tr> 
     <th scope="col">5</th> 
     <th scope="col">MD</th> 
     <th scope="col">PM</th> 
    </tr> 
    <tr> 
     <th scope="col">5</th> 
     <th scope="col">MD</th> 
     <th scope="col">PM</th> 
    </tr> 
    </table > 
    <img src="file:///C:/Users/pc/Downloads/Googletraffic/Tazbet/Autostrad;Helwan To Da2ery/Spiral.jpg " width="700" height="777" alt=""/> 
</description> 

here's我的KML文件

+0

「XML」包允許添加,刪除和編輯標籤,標籤屬性和標籤值。 – hrbrmstr

+0

XML庫可能是你想要的。看到這個帖子 - > http://stackoverflow.com/questions/23505906/use-r-to-read-a-xml-file-select-few-nodes-and-write-it-back-to-another-xml – JMT2080AD

+0

@hrbrmstr,我正在閱讀手冊,我正在尋找一個功能,在特定的地方添加標籤,如果您找到答案,請幫助我。 –

回答

1

我發現離開,但不是那麼有效,我打開NotePad ++上的kml文件,然後獲取root並將其放入xml文件,然後使用此代碼讀取xml,

Url <- "xml_data1.xml" 
data <- xmlTreeParse(Url) 

xmlTreeParse()讓我來解析xml文件的列表,所以我可以在XML文件中添加任何東西到一個特定的地方,這個代碼我用來添加節點

data$doc$children$Folder[[3]][[3]][[3]][["description"]] <- top 

是小心,有XMLInternalElementNodeXMLNode之間的區別,所以你不能直接使用saveXML()這樣的..

saveXML(data, file ="xml_data2.kml") 

你應該得到第一

012數據的根
xmlroot <- xmlRoot(data) 
saveXML(xmlroot, file ="xml_data2.xml") 

這個答案寫XML寫here

那麼你可以使用記事本打開xml_data2.xml ++,並得到你想要什麼,然後把它再次進入KML文件。