使用Sax2方式解析xml

前端之家收集整理的这篇文章主要介绍了使用Sax2方式解析xml前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

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>

将strings.xml放到android工程目录assets下

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 qName)
				throws SAXException {
			Log.d(TAG,"endElement " + " uri:" + uri + " localName:" + localName + " qName:" + qName);
			super.endElement(uri,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,length);
		}
		
	}
}
打印日志:
原文链接:https://www.f2er.com/xml/300148.html

猜你在找的XML相关文章