Hey guys,

I've been working with KML files for displaying placemarks on Google Earth. Through my C++ application I need to insert new placemarks in a generated file. Some example code follows and may give you an indication on what kml is and what i want to do.

As the file with the addition of more and more placemarks may become huge I'm looking for an efficient way of inserting data right after the definition of the styles (so I don't have to read the whole file and then insert the data).

Does anyone know how the use of pointers may help?

Thanks in Advance,

Liza C

<?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:kml="http://www.opengis.net/kml/2.2" xmlns:atom="http://www.w3.org/2005/Atom">
<Document>
  <!-- Begin Style Definitions -->
  <Style id="myDefaultStyles">
    <LabelStyle>
    </LabelStyle>
    <LineStyle>
		<color>61ffffff</color>
		<colorMode>random</colorMode>
		<width>3</width>
	</LineStyle>
	<PolyStyle>
		<color>00ffffff</color>
		<colorMode>random</colorMode>
	</PolyStyle>
  </Style>
  <Style id="icon">
      <IconStyle>
         <color>ffffffff</color>
         <scale>1.1</scale>
         <Icon>
            <href>http://i219.photobucket.com/albums/cc160/eliza2044/Random/icon.png</href>
         </Icon>
      </IconStyle>
   </Style>
  <!-- End Style Definitions -->
  <!-- Placemark #1 -->
  <Placemark>
    <name>Google Earth - New Polygon</name>
    <description>Here is some descriptive text</description>
    <styleUrl>#myDefaultStyles</styleUrl>
	<LineString>
		<extrude>1</extrude>
		<tessellate>1</tessellate>
		<altitudeMode>relativeToGround</altitudeMode>
		<coordinates>
			-1.71999,53.11725,33975
			-1.72226,53.11917,33975
			-1.72413,53.12081,33975
			-1.72603,53.12242,34000
			-1.72980,53.12563,34000
			-1.73164,53.12723,34000
			-1.73356,53.12883,34000
			-1.73506,53.13011,34000
			-1.75127,53.14390,34000
			-1.75652,53.14841,34000
			-1.76213,53.15323,34000
			-1.76370,53.15451,34000
			-1.76557,53.15609,34000
			-1.93174,53.31162,34000
			-1.93238,53.31232,34000
			-1.93399,53.31400,34000
			-1.93469,53.31468,34000
			-1.93561,53.31567,34000
			-1.93626,53.31637,34000
			-1.93691,53.31707,34000
			-1.93791,53.31807,34000
			-1.93884,53.31907,34000
			-1.94042,53.32077,34000
			-1.94143,53.32182,34000
			-1.94238,53.32283,34000
			-1.94301,53.32352,34000
		</coordinates>
	</LineString>
</Placemark>
<!-- Placemark point  -->
<Placemark>
      <name>IconStyle.kml</name>
      <styleUrl>#icon</styleUrl>
      <Point>
		<extrude>1</extrude>                        <!-- boolean -->
		<altitudeMode>relativeToGround</altitudeMode> 
		<coordinates>-1.94301,53.32352,34000</coordinates>
      </Point>
   </Placemark>
  <!-- Placemark #2 -->
  <Placemark>
    <name>Google Earth - New Path</name>
    <styleUrl>#myDefaultStyles</styleUrl>
			<LineString>
				<extrude>1</extrude>
				<tessellate>1</tessellate>
				<altitudeMode>relativeToGround</altitudeMode>
				<coordinates>
					-2.01654,53.45558,2925
					-2.01769,53.45589,2900
					-2.01833,53.45607,2900
					-2.01890,53.45626,2900
					-2.02109,53.45673,2900
					-2.02172,53.45682,2900
					-2.02203,53.45686,2875
					-2.02358,53.45710,2875
					-2.02517,53.45732,2875
					-2.02609,53.45742,2875
					-2.02674,53.45750,2850
					-2.02770,53.45756,2850
					-2.02835,53.45761,2850
					-2.03122,53.45773,2850
					-2.03182,53.45775,2825
					-2.15364,53.41227,1775
					-2.15405,53.41208,1750
					-2.15437,53.41190,1750
					-2.15497,53.41164,1750
					-2.15558,53.41134,1750
					-2.15599,53.41115,1725
					-2.15728,53.41050,1725
					-2.15787,53.41022,1700
					-2.15834,53.40999,1700
				</coordinates>
			</LineString>
  </Placemark>
</Document>
</kml>

Recommended Answers

All 2 Replies

You can't insert into a text file.
Read the whole thing in, make some changes, then write it all back out again.

But as it seems to be XML, there's bound to be some handy class which makes easy work of serialising the data, and with access methods which allow you to traverse the document tree, and make changes to it.

You can't insert into a text file.
Read the whole thing in, make some changes, then write it all back out again.

But as it seems to be XML, there's bound to be some handy class which makes easy work of serialising the data, and with access methods which allow you to traverse the document tree, and make changes to it.

Yes I know that I cannot insert directly data in the file, and this is the reason I'm looking for an efficient way of doing it..! Reading the whole file and then changing it is not a solution as the file may become huge and it will take lots of time and processing power to complete the task.

I thought that I could use the tellg() method of the fstream class to save time and jump to specific lines but I'm not sure if it works.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.