Android App zum manipulieren eines XML Files

theCatalist

Grünschnabel
Android App zum aulesen eines XML Files mit Sax

Hallo zusammen,
ich habe folgende problem stellung: ich habe ein bestehendes xml, von diesem XML File möchte ich die Tags<name> <affichage> <IW> und <wagolink> auslesen und auf dem handy (mobiltelefon, soory bin aus dem nachbarland ;) ) darstellen, mein Problem ist jetzt wie parse ich das File so das es beim Tag <affichcge>button</affichage> das Programm merkt hier muss ein Button dar gestellt werden. Weiter mus dan durch drücken des Buttons der tag <wagolink> ausgelöst werden um den Befehl an die Steuerung zu übergeben.
oder bin ich so auf dem Holz weg? Danke für eure Hilfe******
XML File:
Code:
<?xml version="1.0"?>

   <items>
		<item id="0">
			<title>Temperatur</title>
			<name>Aussentemperatur</name>
			<description>Temperatur1</description>
			<type>lineaire</type>
			<unit>C</unit>
			<coef>1</coef>
			<offset>0</offset>
			<IW><!--#READPI ADR=MW210&FORMAT=%d--></IW>
		</item>
		<item id="1">
			<title>Datum</title>
			<name>Datum</name>
			<description>Aktuelles Datum</description>
			<type>direct</type>
			<affichage>date</affichage>
			<IW><!--#GETTIMEDATE FORMAT=%d.%m.%y %H:%M:%S--></IW>
		</item>
		<item id="2">
			<title>Praesentation</title>
			<name>Praesentation</name>
			<description>Praesentation</description>
			<type>direct</type>
			<affichage>button</affichage>
			<IW><!--#READPI ADR=MW214&FORMAT=%d--></IW>
			<wagolink>http://192.168.92.100/WRITEPI?ADR1=MX200.1amp;VALUE1=1&amp;FORMAT1=%d</wagolink>
		</item>

xmlhandler.java

Code:
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class MyXMLHandler extends DefaultHandler {
	Boolean currentElement = false;
    String currentValue = null;
  public static SitesList sitesList = null;


	public static SitesList getSitesList() {
		return sitesList;
	}

	public static void setSitesList(SitesList sitesList) {
		MyXMLHandler.sitesList = sitesList;
	}

	/** Called when tag starts ( ex:- <name>AndroidPeople</name> 
	 * -- <name> )*/
	@Override
	public void startElement(String uri, String localName, String qName,
			Attributes attributes) throws SAXException {
		currentElement = true;
		
		if (localName.equals("items"))
		{
			/** Start */
			sitesList = new SitesList();
		} else if (localName.equals("item")) {
			/** Get attribute value */
			String attr = attributes.getValue("id");
			sitesList.setid(attr);
		}
		else if (localName.equals("affichage")) 
			sitesList.setaffichage(currentValue);
	 }
		 
	 
	

	/** Called when tag closing ( ex:- <name>AndroidPeople</name> 
	 * -- </name> )*/
	@Override
	public void endElement(String uri, String localName, String qName)
			throws SAXException {
		
		
		
		currentElement = false;
		/** set value */
		if (localName.equalsIgnoreCase("title"))
			sitesList.settitle(currentValue);
		if (localName.equalsIgnoreCase("affichage"))
			
			sitesList.setaffichage(currentValue);
		else if (localName.equalsIgnoreCase("item"))
			sitesList.setitem(currentValue);

	}




	/** Called to get tag characters ( ex:- <name>AndroidPeople</name> 
	 * -- to get AndroidPeople Character ) */
	@Override
	public void characters(char[] ch, int start, int length)
	throws SAXException {
	 if (currentElement) {
		           currentValue = new String(ch, start, length);
		            currentElement = false;
		        }
			 
			    }
			 
	}

xmlparser.java
Code:
public class XMLParsingExample extends Activity {

	/** Create Object For SiteList Class */
	SitesList sitesList = null;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		

		/** Create a new layout to display the view */
		 ScrollView sView =  new ScrollView (this);
		 LinearLayout layout = new LinearLayout(this);
		 layout.setOrientation(LinearLayout.VERTICAL);
		 sView.addView(layout);

		/** Create a new textview array to display the results */
		TextView title[];
		TextView item[];
		TextView id[];
		Button affichage[];
		
		try {
			
			/** Handling XML */
			SAXParserFactory spf = SAXParserFactory.newInstance();
			SAXParser sp = spf.newSAXParser();
			XMLReader xr = sp.getXMLReader();

			/** Send URL to parse XML Tags */
			URL sourceUrl = new URL(
					"http://172.17.1.104/LEHMANN.xml");

			/** Create handler to handle XML Tags ( extends DefaultHandler ) */
			MyXMLHandler myXMLHandler = new MyXMLHandler();
			xr.setContentHandler(myXMLHandler);
			xr.parse(new InputSource(sourceUrl.openStream()));
			
		} catch (Exception e) {
			System.out.println("XML Pasing Excpetion = " + e);
		}

		/** Get result from MyXMLHandler SitlesList Object */
		sitesList = MyXMLHandler.sitesList;

		/** Assign textview array lenght by arraylist size */
		title = new TextView[sitesList.gettitle().size()];
		item = new TextView[sitesList.gettitle().size()];
		id = new TextView[sitesList.gettitle().size()];
		affichage = new Button[sitesList.gettitle().size()];
		
		/** Set the result text in textview and add it to layout */
		for (int i = 0; i < sitesList.gettitle().size(); i++) {
			title[i] = new TextView(this);
			title[i].setText("Name = "+sitesList.gettitle().get(i));
			item[i] = new TextView(this);
			item[i].setText("Website = "+sitesList.getitem().get(i));
			id[i] = new TextView(this);
			id[i].setText("Website Category = "+sitesList.getid().get(i));
			affichage[i] = new Button(this);
			affichage[i].setText("Licht AN/AUS "+sitesList.getaffichage().get(i));

			layout.addView(title[i]);
			layout.addView(item[i]);
			layout.addView(id[i]);
			layout.addView(affichage[i]);
		}

		/** Set the layout view to display */
		this.setContentView(sView);

	}
}

siteslist.java
Code:
import java.util.ArrayList;

/** Contains getter and setter method for varialbles  */
public class SitesList {

	/** Variables */
	private ArrayList<String> title = new ArrayList<String>();
	private ArrayList<String> affichage = new ArrayList<String>();
	private ArrayList<String> item = new ArrayList<String>();
	private ArrayList<String> id = new ArrayList<String>();
	private ArrayList<String> IW = new ArrayList<String>();

	
	/** In Setter method default it will return arraylist 
	 *  change that to add  */
	
	public ArrayList<String> gettitle() {
		return title;
	}

	public void settitle(String title) {
		this.title.add(title);
	}
	
	public ArrayList<String> getaffichage() {
		return affichage;
	}

	public void setaffichage(String affichage) {
		this.affichage.add(affichage);
	}
	public ArrayList<String> getitem() {
		return item;
	}

	public void setitem(String item) {
		this.item.add(item);
	}
	
	public ArrayList<String> getid() {
		return id;
	}

	public void setid(String id) {
		this.id.add(id);
	}
	
	public ArrayList<String> getIW() {
		return IW;
	}

	public void setIW(String IW) {
		this.IW.add(IW);
	}



}
 
Zuletzt bearbeitet:
Zurück