strings.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">Sax2</string> <string name="action_settings">Settings</string> <string name="hello_world">Hello world!</string> <test>test</test> </resources>
MainActivity.java
package com.example.sax2; import java.io.IOException; import java.io.InputStream; import javax.xml.parsers.ParserConfigurationException; import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXParserFactory; import org.xml.sax.Attributes; import org.xml.sax.SAXException; import org.xml.sax.helpers.DefaultHandler; import android.app.Activity; import android.os.Bundle; import android.util.Log; import android.view.View; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); View view = new View(getApplicationContext()); setContentView(view); InputStream inputStream = null; try { inputStream = getAssets().open("strings.xml"); SAXParserFactory factory = SAXParserFactory.newInstance(); SAXParser parser = factory.newSAXParser(); parser.parse(inputStream, new XmlHandler()); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }catch (ParserConfigurationException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SAXException e) { // TODO Auto-generated catch block e.printStackTrace(); } } private class XmlHandler extends DefaultHandler { private static final String TAG = "XmlHandler"; @Override public void startDocument() throws SAXException { Log.d(TAG, "startDocument"); super.startDocument(); } @Override public void endDocument() throws SAXException { Log.d(TAG, "endDocument"); super.endDocument(); } @Override public void startPrefixMapping(String prefix, String uri) throws SAXException { Log.d(TAG, "startPrefixMapping " + " prefix:" + prefix + " uri:" + uri); super.startPrefixMapping(prefix, uri); } @Override public void endPrefixMapping(String prefix) throws SAXException { Log.d(TAG, "endPrefixMapping " + " prefix:" + prefix); super.endPrefixMapping(prefix); } @Override public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { Log.d(TAG, "startElement " + " uri:" + uri + " localName:" + localName + " qName:" + qName + " attributes:" + attributes); if (qName.equals("string")) { String name = attributes.getValue("name"); Log.d(TAG, "startElement " + " name:" + name); } super.startElement(uri, localName, qName, attributes); } @Override public void endElement(String uri, String localName, String qName) throws SAXException { Log.d(TAG, "endElement " + " uri:" + uri + " localName:" + localName + " qName:" + qName); super.endElement(uri, localName, qName); } @Override public void characters(char[] ch, int start, int length) throws SAXException { String string = new String(ch, start, length); Log.d(TAG, "characters " + " ch:" + string + " start:" + start + " length:" + length); super.characters(ch, start, length); } } }打印日志: