I am using JDOM to create and modify a KML file. Every 5 seconds I receive the new values of latitude,longitude and time from the client application. I need to modify the existing file and add the latest values of latitude, longitude and time to it.
The XML file is as given below
<?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">
<Document>
<Folder>
<Placemark>
<name>deviceA</name>
<gx:Track>
<when>2015-06-28T17:02:09Z</when>
<when>2015-06-28T17:02:35Z</when>
<gx:coord>3.404258 50.605892 100.000000</gx:coord>
<gx:coord>3.416446 50.604040 100.000000</gx:coord>
</gx:Track>
</Placemark>
<Placemark>
<name>deviceB</name>
<gx:Track>
<when>2015-06-28T17:02:09Z</when>
<when>2015-06-28T17:02:35Z</when>
<gx:coord>3.403133 50.601702 100.000000</gx:coord>
<gx:coord>3.410171 50.597344 100.000000</gx:coord>
</gx:Track>
</Placemark>
</Folder>
</Document>
</kml>
I use the following code to insert the value
SAXBuilder builder = new SAXBuilder();
File xmlFile = new File(outputFile);
try {
Document doc = (Document) builder.build(xmlFile);
Element rootNode = doc.getRootElement();
Element docNode = rootNode.getChild("Document",ns);
Element folNode = docNode.getChild("Folder",ns);
List list = folNode.getChildren("Placemark",ns);
if(list.size()>0)
{
Element node = (Element) list.get(deviceid);
Element tracknode = node.getChild("Track",ns2);
List wlist = tracknode.getChildren("when",ns);
Element newWhen = new Element("when",ns);
newWhen.setText(whentext);
Element newCoord = new Element("coord",ns2);
newCoord.setText(coordtext);
System.out.println("When size:"+wlist.size());
int index =0;
if(wlist.size()==0) index =0;
else index= wlist.size()+1;
tracknode.addContent(index, newWhen);
tracknode.addContent(newCoord);
}
XMLOutputter outputter = new XMLOutputter(Format.getPrettyFormat());
FileOutputStream writer = new FileOutputStream(outputFile);
outputter.output(doc, writer);
writer.flush();
writer.close();
} catch (IOException io) {
System.out.println(io.getMessage());
} catch (JDOMException jdomex) {
System.out.println(jdomex.getMessage());
}
The 'gx:coord' part is inserted correctly at the end of the elements, but the new when element needs to be inserted at the end of the elements 'when'. So I get the children list with tag 'when'. Get the size of the element list and insert at the index after the last element. The first two insertions are ok, the third insertion onwards I face a weird problem. The new element 'when' is getting inserted in between the existing when elements and not at the end of the list of when elements. For example
<gx:Track>
<when>2015-06-28T17:02:09Z</when>
<when>2015-06-28T17:02:44Z</when>
<when>2015-06-28T17:02:35Z</when>
<gx:coord>3.404258 50.605892 100.000000</gx:coord>
<gx:coord>3.416446 50.604040 100.000000</gx:coord>
<gx:coord>3.429492 50.602078 100.000000</gx:coord>
</gx:Track>
I would like to insert the new 'when' element after all the existing when elements. Is there anyway to do this using JDOM in java?
Any help is appreciated