Reputation: 1166
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
Upvotes: 2
Views: 1158
Reputation: 17707
In JDOM, lists are live, even filtered lists of content that contain just a subset of the items in a parent.
For example, your code to create the element nodes is fine:
Element newWhen = new Element("when",ns);
newWhen.setText(whentext);
Element newCoord = new Element("coord",ns2);
newCoord.setText(coordtext);
But, how about adding them like:
Element firstcoord = tracknode.getChild("coord",ns2);
tracknode.addContent(tracknode.indexOf(firstcoord), newWhen);
tracknode.addContent(newCoord);
If the track is empty, though, you will need a different solution.
Note that you should use generics more in your code. The List values from JDOM are all generics-compliant, and useful. Here's the full (modified) code I have been using to test the above:
Document doc = new SAXBuilder().build("locations.kml");
Namespace ns = Namespace.getNamespace("http://www.opengis.net/kml/2.2");
Namespace ns2 = Namespace.getNamespace("gx", "http://www.google.com/kml/ext/2.2");
Element rootNode = doc.getRootElement();
Element docNode = rootNode.getChild("Document",ns);
Element folNode = docNode.getChild("Folder",ns);
List<Element> list = folNode.getChildren("Placemark",ns);
if(!list.isEmpty())
{
Element node = list.get(0);
Element tracknode = node.getChild("Track",ns2);
Element newWhen = new Element("when",ns);
newWhen.setText("WHEN");
Element newCoord = new Element("coord",ns2);
newCoord.setText("WHERE");
Element firstcoord = tracknode.getChild("coord",ns2);
tracknode.addContent(tracknode.indexOf(firstcoord), newWhen);
tracknode.addContent(newCoord);
}
XMLOutputter outputter = new XMLOutputter(Format.getPrettyFormat());
outputter.output(doc, System.out);
Upvotes: 2