android 解析xml



android 解析xml。

 
Java代码  
  1. package mars.xml;   
  2.   
  3. import java.io.StringReader;   
  4.   
  5. import javax.xml.parsers.SAXParserFactory;   
  6.   
  7. import mars.utils.HttpDownloader;   
  8.   
  9. import org.xml.sax.InputSource;   
  10. import org.xml.sax.XMLReader;   
  11.   
  12. import android.app.Activity;   
  13. import android.os.Bundle;   
  14. import android.view.View;   
  15. import android.view.View.OnClickListener;   
  16. import android.widget.Button;   
  17.   
  18. public class XMLActitity extends Activity {   
  19.     /** Called when the activity is first created. */  
  20.     private Button parseButton ;   
  21.     @Override  
  22.     public void onCreate(Bundle savedInstanceState) {   
  23.         super.onCreate(savedInstanceState);   
  24.         setContentView(R.layout.main);   
  25.         parseButton = (Button)findViewById(R.id.parseButton);   
  26.         parseButton.setOnClickListener(new ParseButtonListener());   
  27.     }   
  28.        
  29.     class ParseButtonListener implements OnClickListener{   
  30.   
  31.         @Override  
  32.         public void onClick(View v) {   
  33.             HttpDownloader hd = new HttpDownloader();   
  34.             String resultStr = hd.download(“http://192.168.1.107:8081/voa1500/test.xml”);   
  35.             System.out.println(resultStr);   
  36.             try{   
  37.                 //创建一个SAXParserFactory   
  38.                 SAXParserFactory factory = SAXParserFactory.newInstance();   
  39.                 XMLReader reader = factory.newSAXParser().getXMLReader();   
  40.                 //为XMLReader设置内容处理器   
  41.                 reader.setContentHandler(new MyContentHandler());   
  42.                 //开始解析文件   
  43.                 reader.parse(new InputSource(new StringReader(resultStr)));   
  44.             }   
  45.             catch(Exception e){   
  46.                 e.printStackTrace();   
  47.             }   
  48.         }   
  49.            
  50.     }   
  51. }  
package mars.xml;

import java.io.StringReader;

import javax.xml.parsers.SAXParserFactory;

import mars.utils.HttpDownloader;

import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class XMLActitity extends Activity {
    /** Called when the activity is first created. */
	private Button parseButton ;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        parseButton = (Button)findViewById(R.id.parseButton);
        parseButton.setOnClickListener(new ParseButtonListener());
    }

    class ParseButtonListener implements OnClickListener{

		@Override
		public void onClick(View v) {
			HttpDownloader hd = new HttpDownloader();
			String resultStr = hd.download("http://192.168.1.107:8081/voa1500/test.xml");
			System.out.println(resultStr);
			try{
				//创建一个SAXParserFactory
				SAXParserFactory factory = SAXParserFactory.newInstance();
				XMLReader reader = factory.newSAXParser().getXMLReader();
				//为XMLReader设置内容处理器
				reader.setContentHandler(new MyContentHandler());
				//开始解析文件
				reader.parse(new InputSource(new StringReader(resultStr)));
			}
			catch(Exception e){
				e.printStackTrace();
			}
		}

    }
}
Java代码  
  1. package mars.xml;   
  2.   
  3. import org.xml.sax.Attributes;   
  4. import org.xml.sax.SAXException;   
  5. import org.xml.sax.helpers.DefaultHandler;   
  6.   
  7. public class MyContentHandler extends DefaultHandler {   
  8.     String hisname, address, money, sex, status;   
  9.     String tagName;   
  10.   
  11.     public void startDocument() throws SAXException {   
  12.         System.out.println(“““““begin““““”);   
  13.     }   
  14.   
  15.     public void endDocument() throws SAXException {   
  16.         System.out.println(“““““end““““”);   
  17.     }   
  18.   
  19.     public void startElement(String namespaceURI, String localName,   
  20.             String qName, Attributes attr) throws SAXException {   
  21.         tagName = localName;   
  22.         if (localName.equals(“worker”)) {   
  23.             //获取标签的全部属性   
  24.             for (int i = 0; i < attr.getLength(); i++) {   
  25.                 System.out.println(attr.getLocalName(i) + ”=” + attr.getValue(i));   
  26.             }   
  27.         }   
  28.     }   
  29.   
  30.     public void endElement(String namespaceURI, String localName, String qName)   
  31.             throws SAXException {   
  32.         //在workr标签解析完之后,会打印出所有得到的数据   
  33.         tagName = ”";   
  34.         if (localName.equals(“worker”)) {   
  35.             this.printout();   
  36.         }   
  37.     }   
  38.     public void characters(char[] ch, int start, int length)   
  39.             throws SAXException {   
  40.         if (tagName.equals(“name”))   
  41.             hisname = new String(ch, start, length);   
  42.         else if (tagName.equals(“sex”))   
  43.             sex = new String(ch, start, length);   
  44.         else if (tagName.equals(“status”))   
  45.             status = new String(ch, start, length);   
  46.         else if (tagName.equals(“address”))   
  47.             address = new String(ch, start, length);   
  48.         else if (tagName.equals(“money”))   
  49.             money = new String(ch, start, length);   
  50.     }   
  51.   
  52.     private void printout() {   
  53.         System.out.print(“name: ”);   
  54.         System.out.println(hisname);   
  55.         System.out.print(“sex: ”);   
  56.         System.out.println(sex);   
  57.         System.out.print(“status: ”);   
  58.         System.out.println(status);   
  59.         System.out.print(“address: ”);   
  60.         System.out.println(address);   
  61.         System.out.print(“money: ”);   
  62.         System.out.println(money);   
  63.         System.out.println();   
  64.     }   
  65.   
  66. }  
package mars.xml;

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

public class MyContentHandler extends DefaultHandler {
	String hisname, address, money, sex, status;
	String tagName;

	public void startDocument() throws SAXException {
		System.out.println("````````begin````````");
	}

	public void endDocument() throws SAXException {
		System.out.println("````````end````````");
	}

	public void startElement(String namespaceURI, String localName,
			String qName, Attributes attr) throws SAXException {
		tagName = localName;
		if (localName.equals("worker")) {
			//获取标签的全部属性
			for (int i = 0; i < attr.getLength(); i++) {
				System.out.println(attr.getLocalName(i) + "=" + attr.getValue(i));
			}
		}
	}

	public void endElement(String namespaceURI, String localName, String qName)
			throws SAXException {
		//在workr标签解析完之后,会打印出所有得到的数据
		tagName = "";
		if (localName.equals("worker")) {
			this.printout();
		}
	}
	public void characters(char[] ch, int start, int length)
			throws SAXException {
		if (tagName.equals("name"))
			hisname = new String(ch, start, length);
		else if (tagName.equals("sex"))
			sex = new String(ch, start, length);
		else if (tagName.equals("status"))
			status = new String(ch, start, length);
		else if (tagName.equals("address"))
			address = new String(ch, start, length);
		else if (tagName.equals("money"))
			money = new String(ch, start, length);
	}

	private void printout() {
		System.out.print("name: ");
		System.out.println(hisname);
		System.out.print("sex: ");
		System.out.println(sex);
		System.out.print("status: ");
		System.out.println(status);
		System.out.print("address: ");
		System.out.println(address);
		System.out.print("money: ");
		System.out.println(money);
		System.out.println();
	}

}