xml - Insert a new element in between elements in KML using JDOM in java -


i using jdom create , modify kml file. every 5 seconds receive new values of latitude,longitude , time client application. need modify existing file , add latest values of latitude, longitude , time it.

the xml file 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 following code insert 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 inserted correctly @ end of elements, new when element needs inserted @ end of elements 'when'. children list tag 'when'. size of element list , insert @ index after last element. first 2 insertions ok, third insertion onwards face weird problem. new element 'when' getting inserted in between existing when elements , not @ end of list of when elements. 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 insert new 'when' element after existing when elements. there anyway using jdom in java?

any appreciated

in jdom, lists live, filtered lists of content contain subset of items in parent.

for example, code create element nodes fine:

        element newwhen = new element("when",ns);         newwhen.settext(whentext);          element newcoord = new element("coord",ns2);         newcoord.settext(coordtext); 

but, how adding them like:

        element firstcoord = tracknode.getchild("coord",ns2);         tracknode.addcontent(tracknode.indexof(firstcoord), newwhen);         tracknode.addcontent(newcoord); 
  • add when before first coordinate.
  • add @ end.

if track empty, though, need different solution.

note should use generics more in code. list values jdom generics-compliant, , useful. here's full (modified) code have been using test 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); 

Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

Rendering JButton to get the JCheckBox behavior in a JTable by using images does not update my table -