[Simple-support] How to append elements to existing XML file
Brought to you by:
niallg
|
From: Cody D D. <cdd...@st...> - 2012-01-16 20:42:02
|
Hello, I have been trying to figure this out for some time now and have decided its time to consult the mailing list. My issue deals with the creation of a GPX file for GPS use in Android. The basic structure that I am wanting for my XML file is shown below. I have achieved the structure using Simple, but cannot figure out how to only add additional trkpt elements when I update the file. Every time my android device receives a GPS location I need to just write another trkpt to the existing xml file, but my program is repeating the entire structure shown below with each new location. I am assuming there is a way to append just a single element to an already existing XML file and have it maintain the tree structure (just like the two trkpts shown below). I have also placed a copy of the code that is currently run every time my application receives a new location point below. Thanks for any help you can offer! Cody File Structure <?xml version="1.0" encoding="UTF-8"?> <gpx xmlns="http://www.topografix.com/GPX/1/1" version="1.1" creator="Cody" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.topografix.com/GPX/1/1/gpx.xsd"> <trk> <trkseg> <trkpt lon="9.860624216140083" lat="54.9328621088893"> <ele>228.0</ele> <time>2011-12-20T22:46:16Z</time> <speed>13.0</speed> <src>gps</src> </trkpt> <trkpt lon="9.860624216140083" lat="54.9328621088893"> <ele>228.0</ele> <time>2011-12-20T22:46:16Z</time> <speed>13.0</speed> <src>gps</src> </trkpt> </trkseg> </trk> </gpx> Current Writing Code String prolog = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"; Format format = new Format(prolog); Serializer serializer = new Persister(format); Gpx gpx = new Gpx(); Trk trk = new Trk(); gpx.setTrk(trk); Trkseg trkseg = new Trkseg(); trk.setTrkseg(trkseg); Trkpt trkpt = new Trkpt(); trkpt.setLon(String.valueOf(loc.getLongitude())); trkpt.setLat(String.valueOf(loc.getLatitude())); trkpt.setEle(String.valueOf(loc.getAltitude())); trkpt.setTime(dateTimeString); trkpt.setSpeed(String.valueOf(loc.getSpeed())); trkpt.setSrc(loc.getProvider()); trkseg.setTrkpt(trkpt); FileWriter fstream = new FileWriter(gpxFile, true); BufferedWriter out = new BufferedWriter(fstream); serializer.write(gpx, out); |