SAX是一種佔用記憶體少且解析速度快的解析器,它採用的是事件啟動,它不需要解析完整個文檔,而是按照內容順序 看文檔某個部分是否符合xml文法,如果符合就觸發相應的事件,所謂的事件就是些回調方法(callback),這些方法 定義在ContentHandler中,下面是其主要方法:
startDocument:當遇到文檔的時候就觸發這個事件 調用這個方法 可以在其中做些預先處理工作startElement: (String namespaceURI,String localName,String qName,Attributes atts)當遇開始標籤的時候就會觸發這個方法。endElement(String uri,String localName,String name):當遇到結束標籤時觸發這個事件,調用此法可以做些善後工作。charachers(char [] ch,int start,int length):當遇到xml內容時觸發這個方法,用new String(ch,start,length)可以接受內容。MyContentHandler.java
package mars.xml;</p><p>import org.xml.sax.Attributes;<br />import org.xml.sax.SAXException;<br />import org.xml.sax.helpers.DefaultHandler;</p><p>public class MyContentHandler extends DefaultHandler {<br />String hisname, address, money, sex, status;<br />String tagName;</p><p>public void startDocument() throws SAXException {<br />System.out.println("````````begin````````");<br />}</p><p>public void endDocument() throws SAXException {<br />System.out.println("````````end````````");<br />}</p><p>public void startElement(String namespaceURI, String localName,<br />String qName, Attributes attr) throws SAXException {<br />tagName = localName;<br />if (localName.equals("worker")) {<br />//擷取標籤的全部屬性<br />for (int i = 0; i < attr.getLength(); i++) {<br />System.out.println(attr.getLocalName(i) + "=" + attr.getValue(i));<br />}<br />}<br />}</p><p>public void endElement(String namespaceURI, String localName, String qName)<br />throws SAXException {<br />//在workr標籤解析完之後,會列印出所有得到的資料<br />tagName = "";<br />if (localName.equals("worker")) {<br />this.printout();<br />}<br />}<br />public void characters(char[] ch, int start, int length)<br />throws SAXException {<br />if (tagName.equals("name"))<br />hisname = new String(ch, start, length);<br />else if (tagName.equals("sex"))<br />sex = new String(ch, start, length);<br />else if (tagName.equals("status"))<br />status = new String(ch, start, length);<br />else if (tagName.equals("address"))<br />address = new String(ch, start, length);<br />else if (tagName.equals("money"))<br />money = new String(ch, start, length);<br />}</p><p>private void printout() {<br />System.out.print("name: ");<br />System.out.println(hisname);<br />System.out.print("sex: ");<br />System.out.println(sex);<br />System.out.print("status: ");<br />System.out.println(status);<br />System.out.print("address: ");<br />System.out.println(address);<br />System.out.print("money: ");<br />System.out.println(money);<br />System.out.println();<br />}</p><p>}<br />
XMLActitity.javapackage mars.xml;</p><p>import java.io.StringReader;</p><p>import javax.xml.parsers.SAXParserFactory;</p><p>import mars.utils.HttpDownloader;</p><p>import org.xml.sax.InputSource;<br />import org.xml.sax.XMLReader;</p><p>import android.app.Activity;<br />import android.os.Bundle;<br />import android.view.View;<br />import android.view.View.OnClickListener;<br />import android.widget.Button;</p><p>public class XMLActitity extends Activity {<br /> /** Called when the activity is first created. */<br />private Button parseButton ;<br /> @Override<br /> public void onCreate(Bundle savedInstanceState) {<br /> super.onCreate(savedInstanceState);<br /> setContentView(R.layout.main);<br /> parseButton = (Button)findViewById(R.id.parseButton);<br /> parseButton.setOnClickListener(new ParseButtonListener());<br /> }</p><p> class ParseButtonListener implements OnClickListener{</p><p>public void onClick(View v) {<br />HttpDownloader hd = new HttpDownloader();<br />String resultStr = hd.download("http://192.168.1.107:8081/voa1500/test.xml");<br />System.out.println(resultStr);<br />try{<br />//建立一個SAXParserFactory<br />SAXParserFactory factory = SAXParserFactory.newInstance();<br />XMLReader reader = factory.newSAXParser().getXMLReader();<br />//為XMLReader設定內容處理器<br />reader.setContentHandler(new MyContentHandler());<br />//開始解析檔案<br />reader.parse(new InputSource(new StringReader(resultStr)));<br />}<br />catch(Exception e){<br />e.printStackTrace();<br />}<br />}</p><p> }<br />}