с SAX:
GpxImportActivity.java
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.XMLReaderFactory;
import android.app.Activity;
import android.location.Location;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.Menu;
public class GpxImportActivity extends Activity {
private String fileName ="gpsTrack.gpx";
private File sdCard;
private List<Location> locationList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_gpx_import);
locationList = new ArrayList<Location>();
sdCard = Environment.getExternalStorageDirectory();
if (!sdCard.exists() || !sdCard.canRead()) {
Log.d("GpxImportActivity", "SD-Card not available or not readable");
finish();
}
boolean availableFile = new File(sdCard, fileName).exists();
if (!availableFile) {
Log.d("GpxImportActivity", "File \"" +fileName+ "\" not available");
finish();
} else {
Log.d("GpxImportActivity", "File \"" +fileName+ "\" found");
}
try {
System.setProperty("org.xml.sax.driver","org.xmlpull.v1.sax2.Driver");
XMLReader xmlReader = XMLReaderFactory.createXMLReader();
/*
SAXParserFactory spf = SAXParserFactory.newInstance();
spf.setNamespaceAware(true);
spf.setValidating(false);
SAXParser saxParser = spf.newSAXParser();
XMLReader xmlReader = saxParser.getXMLReader();
*/
GpxFileContentHandler gpxFileContentHandler = new GpxFileContentHandler();
xmlReader.setContentHandler(gpxFileContentHandler);
FileReader fileReader = new FileReader(new File(sdCard,fileName));
InputSource inputSource = new InputSource(fileReader);
xmlReader.parse(inputSource);
locationList = gpxFileContentHandler.getLocationList();
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.gpx_import, menu);
return true;
}
}
GpxFileContentHandler.java
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.xml.sax.Attributes;
import org.xml.sax.ContentHandler;
import org.xml.sax.Locator;
import org.xml.sax.SAXException;
import android.location.Location;
public class GpxFileContentHandler implements ContentHandler {
private String currentValue;
private Location location;
private List<Location> locationList;
private final SimpleDateFormat GPXTIME_SIMPLEDATEFORMAT = new SimpleDateFormat(
"yyyy-MM-dd'T'HH:mm:ss'Z'");
public GpxFileContentHandler() {
locationList = new ArrayList<Location>();
}
public List<Location> getLocationList() {
return locationList;
}
@Override
public void startElement(String uri, String localName, String qName,
Attributes atts) throws SAXException {
if (localName.equalsIgnoreCase("trkpt")) {
location = new Location("gpxImport");
location.setLatitude(Double.parseDouble(atts.getValue("lat").trim()));
location.setLongitude(Double.parseDouble(atts.getValue("lon").trim()));
}
}
@Override
public void endElement(String uri, String localName, String qName)
throws SAXException {
if (localName.equalsIgnoreCase("ele")) {
location.setAltitude(Double.parseDouble(currentValue.trim()));
}
if (localName.equalsIgnoreCase("time")) {
try {
Date date = GPXTIME_SIMPLEDATEFORMAT.parse(currentValue.trim());
Long time = date.getTime();
location.setTime(time);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (localName.equalsIgnoreCase("trkpt")) {
locationList.add(location);
}
}
@Override
public void characters(char[] ch, int start, int length)
throws SAXException {
currentValue = new String(ch, start, length);
}
@Override
public void startDocument() throws SAXException {
// TODO Auto-generated method stub
}
@Override
public void endDocument() throws SAXException {
// TODO Auto-generated method stub
}
@Override
public void endPrefixMapping(String prefix) throws SAXException {
// TODO Auto-generated method stub
}
@Override
public void ignorableWhitespace(char[] ch, int start, int length)
throws SAXException {
// TODO Auto-generated method stub
}
@Override
public void processingInstruction(String target, String data)
throws SAXException {
// TODO Auto-generated method stub
}
@Override
public void setDocumentLocator(Locator locator) {
// TODO Auto-generated method stub
}
@Override
public void skippedEntity(String name) throws SAXException {
// TODO Auto-generated method stub
}
@Override
public void startPrefixMapping(String prefix, String uri)
throws SAXException {
// TODO Auto-generated method stub
}
}