1

I've kml file exported from Google Earth Pro, consist of 37 Folder, each Folder contain number of "minor" Folder, the number of total "minor" Folders is 168, each "minor" has 3 placemarks.

I've HTML code, i made it with R, and want to import this kml file into R and put this HTML code into the first "Placemark" for each "minor" Folder, this HTML code isn't constant, it has variables like the values in the table in this code, and this variables will attached from dataframe i made for this "minor" Folder,when i edit this HTML code, i'll put it into the first "placemark" for this "minor" Folder, and so on for the other "minor" Folders.

is there any fuction in R that can add this html code to the kml file ?

here's the code of "description" in 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 

here's the output in console

<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 my kml file

  • The `XML` package enables adding, removing and editing tags, tag attributes and tag values. – hrbrmstr Sep 21 '16 at 18:09
  • The XML library may be what you want. See this post -> http://stackoverflow.com/questions/23505906/use-r-to-read-a-xml-file-select-few-nodes-and-write-it-back-to-another-xml – JMT2080AD Sep 21 '16 at 18:15
  • @hrbrmstr, i'm reading the manual , i'm looking for a function that add a tag in a specific place, please if you find an answer, help me . – Omar Abd El-Naser Sep 21 '16 at 21:00
  • @JMT_2080AD, that's not what i'm searching for, it creates but not adding. – Omar Abd El-Naser Sep 21 '16 at 21:02
  • http://svitsrv25.epfl.ch/R-doc/library/XML/html/append.XMLNode.html – hrbrmstr Sep 21 '16 at 21:02
  • @JMT_2080AD, is there any way to make R read my code for description ? instead of writing the code from begging on R ? i tried the site u sent, it was useful but i'm not familiar with xml and html so i was writting what i want on Dream Weaver then get that code – Omar Abd El-Naser Sep 21 '16 at 21:31
  • or i should write it better to edit it when i want ? – Omar Abd El-Naser Sep 21 '16 at 21:31
  • @Alsqer If you want to parse KML you need to learn about the DOM. It's not that hard to pick up the basics. It will be time well spent. If I have time later I will see if I can write up an R script for you. I will be a good learning experience for me too. I've been working with more XML type documents lately. – JMT2080AD Sep 21 '16 at 21:52
  • @JMT_2080AD thanks, i'll learn about DOM ... BTW i learnt how to make the above code in R so i can edit what i want in this code throw R . – Omar Abd El-Naser Sep 21 '16 at 22:01
  • @hrbrmstr, thanks alot, now i've to find a way to put my code in a specific placemark at each minor folder – Omar Abd El-Naser Sep 21 '16 at 22:02
  • @hrbrmstr, i can't find a function that convert a nested list to xml , Please do u know something that can do that ? – Omar Abd El-Naser Sep 22 '16 at 17:15
  • http://stackoverflow.com/questions/6256064/how-to-create-xml-from-r-objects-e-g-is-there-a-listtoxml-function (is it really that difficult to search SO?) – hrbrmstr Sep 22 '16 at 17:18
  • @hrbrmstr, my list is a nested list, and i tried the last solution which has 2 ups, it was good but the problem is the output has something wrong, because it's repeat only one folder for so so many times. i can upload the output and let u see it – Omar Abd El-Naser Sep 22 '16 at 17:21
  • @hrbrmstr, could you check my second question ? http://stackoverflow.com/questions/39642725/how-to-convert-nested-list-to-xml-using-r here's what the final result ... but i cant convert it to xml, xml <- listToXml(xml_data,tag = "All Maps") you can see the output. – Omar Abd El-Naser Sep 22 '16 at 17:25

1 Answers1

1

i found away but not so efficient, i open the kml file on NotePad++, then get the root and put it in xml file, then read the xml using this code,

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

xmlTreeParse() allowed me to parse the xml file as a list, so i could add any thing to a specific place in the xml file, and this the code i used to add the node

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

be careful that there is difference between XMLInternalElementNode and XMLNode, so you can't use saveXML() directly like this ..

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

you should get the Root of data first

xmlroot <- xmlRoot(data)
saveXML(xmlroot, file ="xml_data2.xml")

this answer for writing the xml was written here

then you can open the xml_data2.xml using NotePad++ and get what you want then put it again into the kml file.

Community
  • 1
  • 1