Okay.. i found a way.. but its realy not that nice.
The problem is the encoding. If you change it to UTF-8 all is fine but
i guess you have no influence on the xml creation so i created a method
that changes encoding on the fly.
Example follows...
XmlParser.java
Using java Syntax Highlighting
package de.padde.xmlparser;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class XmlParser extends Activity {
public static String lol = ""; //because i'am lazy <img src="http://www.anddev.org/images/smilies/smile.png" alt=":)" title="Smile" />
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//i downloaded the XML you provided and imported it to the res/raw folder
XmlReader.read(getResources().openRawResource(R.raw.mensa));
TextView tv = (TextView) findViewById(R.id.text);
tv.setText(lol);
}
}
Parsed in 0.037 seconds, using
GeSHi 1.0.8.4
XmlReader.java
Using java Syntax Highlighting
package de.padde.xmlparser;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.StringReader;
import java.io.UnsupportedEncodingException;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
public class XmlReader {
public static void read(InputStream in) {
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp;
try {
sp = spf.newSAXParser();
XMLReader xr = sp.getXMLReader();
XmlContentHandler ach = new XmlContentHandler();
xr.setContentHandler(ach);
xr.parse(changeEncoding(in,"windows-1252","utf-8"));
} catch (ParserConfigurationException e) {
} catch (SAXException e) {
} catch (IOException e) {}
}
private static InputSource changeEncoding(InputStream in, String from, String to) {
String f = from.toLowerCase();
String t = to.toLowerCase();
try {
BufferedReader br = new BufferedReader(new InputStreamReader(in,f));
StringBuilder sb = new StringBuilder();
String l = br.readLine().toLowerCase().replace(f, t);
sb.append(l+"n");
while ((l = br.readLine()) != null) sb.append(l+"n");
br.close();
return new InputSource(new StringReader(sb.toString()));
} catch (UnsupportedEncodingException e) {
} catch (IOException e) {}
return null;
}
}
Parsed in 0.044 seconds, using
GeSHi 1.0.8.4
XmlContentHandler.java
Using java Syntax Highlighting
package de.padde.xmlparser;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
public class XmlContentHandler extends DefaultHandler {
private enum Tags {
tag, menue, menu, speisentyp, text, beilage, preis, ausgabe, datum, wochentag,
stand, bis, von, plan, kw, planbezeichnung, filiale
}
private boolean in_menuTag = false;
public void startDocument() throws SAXException { }
public void endDocument() throws SAXException { }
public void startElement(String n, String l, String q, Attributes a) {
switch(Tags.valueOf(l)) {
case menu:
in_menuTag = true;
break;
}
}
public void endElement(String n, String l, String q) {
switch(Tags.valueOf(l)) {
case menu:
in_menuTag = false;
break;
}
}
public void characters(char ch[], int start, int length) {
if(in_menuTag) {
XmlParser.lol += new String(ch,start,length) + "n";
}
}
}
Parsed in 0.040 seconds, using
GeSHi 1.0.8.4
main.xml
Using xml Syntax Highlighting
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="@+id/text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
Parsed in 0.002 seconds, using
GeSHi 1.0.8.4
Note:
The XmlContentHandler just parse menu titles because they contain already a special character (ü)
and because i'm lazy
