my code stock.com

Snippet options

Download: Download snippet as xml-lesen-und-speichern.java.
Copy snippet: For this you need a free my code stock.com account.
Embed code : You will find the embed code for this snippet at the end of the page, if you want to embed it into a website or a blog!

package de.schnti.dhbw.gpsapp.xml;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
import org.xmlpull.v1.XmlSerializer;

import android.location.Location;
import android.os.Environment;
import android.sax.Element;
import android.sax.EndElementListener;
import android.sax.RootElement;
import android.sax.StartElementListener;
import android.util.Xml;
import de.schnti.dhbw.gpsapp.adt.Place;

public class XML extends DefaultHandler {
	private static final String xmlFileName = "places.xml";

	private ArrayList<Place> placeArrayList = new ArrayList<Place>();
	private Place tempPlace;

	/**
	 * 
	 * Store places from XML to ArrayList<Place>
	 * 
	 * @author Timo Schneider ([email protected])
	 * @Date 23.06.2012
	 * 
	 * @return ArrayList<Place>
	 */
	public ArrayList<Place> readPlacesFromXml() throws FileNotFoundException {

		File xmlFile = new File(Environment.getExternalStorageDirectory(),
				xmlFileName);
		FileInputStream fileInputStream = new FileInputStream(xmlFile);

		RootElement root = new RootElement("places");
		Element place = root.getChild("place");

		place.setStartElementListener(new StartElementListener() {
			public void start(Attributes attributes) {

				Location location = new Location("");
				try {
					location.setLatitude(Double.parseDouble(attributes
							.getValue("latitude")));
					location.setLongitude(Double.parseDouble(attributes
							.getValue("longitude")));
					location.setAccuracy(Float.parseFloat(attributes
							.getValue("accuracy")));
				}
				catch (Exception e) {
					// TODO: handle exception
				}
				
				tempPlace = new Place();

				tempPlace.setName(attributes.getValue("name"));
				tempPlace.setStatus(Integer.parseInt(attributes.getValue("status")));
				tempPlace.setLocation(location);
			}
		});

		place.setEndElementListener(new EndElementListener() {
			public void end() {
				placeArrayList.add(tempPlace);
			}
		});

		try {
			Xml.parse(fileInputStream, Xml.Encoding.UTF_8,
					root.getContentHandler());
		} catch (SAXException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return placeArrayList;
	}

	/**
	 * 
	 * Store places from ArrayList<Place> to XML
	 * 
	 * @author Timo Schneider ([email protected])
	 * @Date 23.06.2012
	 * 
	 * @param placesArrayList
	 */
	public void savePlacesToXml(ArrayList<Place> placesArrayList) {
		File xmlFile = new File(Environment.getExternalStorageDirectory(),
				xmlFileName);
		try {
			xmlFile.createNewFile();

			FileOutputStream fileOutputStream = null;
			try {
				fileOutputStream = new FileOutputStream(xmlFile);

				XmlSerializer xmlSerializer = Xml.newSerializer();
				try {
					xmlSerializer.setOutput(fileOutputStream, "UTF-8");
					xmlSerializer.startDocument(null, true);
					xmlSerializer
							.setFeature(
									"http://xmlpull.org/v1/doc/features.html#indent-output",
									true);
					xmlSerializer.startTag(null, "places");

					for (Place place : placesArrayList) {
						xmlSerializer.startTag(null, "place");
						xmlSerializer.attribute(null, "name", place.getName());
						xmlSerializer.attribute(null, "status",
								"" + place.getStatus());
						if (place.getLocation() != null) {
							xmlSerializer.attribute(null, "latitude", String.valueOf(place.getLocation().getLatitude()));
							xmlSerializer.attribute(null, "longitude", String.valueOf(place.getLocation().getLongitude()));
							xmlSerializer.attribute(null, "accuracy", String.valueOf(place.getLocation().getAccuracy()));
						} else {
							xmlSerializer.attribute(null, "latitude", "null");
							xmlSerializer.attribute(null, "longitude", "null");
							xmlSerializer.attribute(null, "accuracy", "null");
						}
						xmlSerializer.endTag(null, "place");
					}
					xmlSerializer.endTag(null, "places");
					xmlSerializer.endDocument();
					xmlSerializer.flush();
					fileOutputStream.close();

				} catch (Exception e) {
					throw new RuntimeException(e);
				}
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}

Create a free my code stock.com account now.

my code stok.com is a free service, which allows you to save and manage code snippes of any kind and programming language. We provide many advantages for your daily work with code-snippets, also for your teamwork. Give it a try!

Find out more and register now

You can customize the height of iFrame-Codes as needed! You can find more infos in our API Reference for iframe Embeds.