Android SDK只支持采用SAX技術讀取XML,SAX采用順序讀取的方式來處理XML文檔。這就要求在每讀取XML文檔的某個節點時會觸發相應的事件來處理這個節點。下面基於一個實例講述SAX的使用:
public class Book { private String name; private String id; private String price; private String publisher; private int count; .... get,set方法省略 }
XML文件如下:
12 10 21
XMLTool.java
1.構建一個工廠SAXParserFactory
2.構建並實例化SAXPraser對象
public class XMLTool { private static SAXParser getSAXParser() throws ParserConfigurationException, SAXException { SAXParserFactory parserFactory = SAXParserFactory.newInstance(); return parserFactory.newSAXParser(); } public static DefaultHandler parse(InputStream inStream,DefaultHandler handler){ if(inStream!=null){ try { SAXParser parser = getSAXParser(); parser.parse(inStream, handler); return handler; } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ if(inStream!=null){ try { inStream.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } return null; } }
BookXMLParser.java
public class BookXMLParser extends DefaultHandler { private ArrayListdataList; private Book book; private StringBuffer stringBuffer = new StringBuffer(); //private StringBuffer buffer=new StringBuffer(); public ArrayList getData() { return dataList; } public void startDocument() throws SAXException { // TODO Auto-generated method stub dataList = new ArrayList (); } public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { // TODO Auto-generated method stub if(qName.equals("book")) { book = new Book(); book.setName(attributes.getValue("book:name")); book.setId(attributes.getValue("book:id")); book.setPrice(attributes.getValue("book:price")); book.setPublisher(attributes.getValue("book:publisher")); } super.startElement(uri, localName, qName, attributes); } @Override public void characters(char[] ch, int start, int length) throws SAXException { // TODO Auto-generated method stub stringBuffer.append(ch,start,length); super.characters(ch, start, length); } @Override public void endElement(String uri, String localName, String qName) throws SAXException { // TODO Auto-generated method stub if(qName.equals("book")) { if(stringBuffer.toString()!=null && !stringBuffer.toString().equals("")) { book.setCount(Integer.parseInt(stringBuffer.toString().trim())); stringBuffer.setLength(0);//必須清空緩沖區 } dataList.add(book); } super.endElement(uri, localName, qName); } }
1.開始分析XML文件。該分析點表示SAX引擎剛剛開始處理XML文件,但是還沒有讀取XML文件中的內容,該分析點對應:
public void startDocument() throws SAXException { // TODO Auto-generated method stub dataList = new ArrayList(); }
2.開始處理每一個XML元素。也就是遇到
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { // TODO Auto-generated method stub if(qName.equals("book")) { book = new Book(); book.setName(attributes.getValue("book:name")); book.setId(attributes.getValue("book:id")); book.setPrice(attributes.getValue("book:price")); book.setPublisher(attributes.getValue("book:publisher")); } super.startElement(uri, localName, qName, attributes); }
public void endElement(String uri, String localName, String qName) throws SAXException { // TODO Auto-generated method stub if(qName.equals("book")) { if(stringBuffer.toString()!=null && !stringBuffer.toString().equals("")) { book.setCount(Integer.parseInt(stringBuffer.toString().trim())); stringBuffer.setLength(0);//必須清空緩沖區 } dataList.add(book); } super.endElement(uri, localName, qName); }
5.讀取字符分析點。這是一個很重要的分析點。如果沒有這個分析點, 前面的工作相當於白做,雖然掃描了XML文件,但是沒有保存.....而這個分析點所對應的characters事件方法的主要作用就是保存SAX讀取的XML文件內容。具體的說就是
public void characters(char[] ch, int start, int length) throws SAXException { // TODO Auto-generated method stub stringBuffer.append(ch,start,length); super.characters(ch, start, length); }
使用SAX解析XML:
public class MainActivity extends Activity { private Listbooks; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); InputStream inStream = getResources().openRawResource(R.raw.books); BookXMLParser parser = new BookXMLParser(); books = ((BookXMLParser)XMLTool.parse(inStream, parser)).getData(); if(books!=null && books.size()>0) { for(int i = 0;i
寫XML文件
public static void WriteXML(Listbooks, OutputStream out) throws Exception { XmlSerializer serializer = Xml.newSerializer(); serializer.setOutput(out, "UTF-8"); serializer.startDocument("UTF-8", true); serializer.startTag(null, "books"); for (Book book : books) { serializer.startTag(null, "book"); serializer.attribute(null, "book:name", book.getName()); serializer.attribute(null, "book:id",book.getId()); serializer.attribute(null, "book:price", book.getPrice()); serializer.attribute(null, "book:publisher",book.getPublisher()); serializer.text(String.valueOf(book.getCount())); serializer.endTag(null, "book"); } serializer.endTag(null, "books"); serializer.endDocument(); out.flush(); out.close(); }
Demo:http://download.csdn.net/detail/tangnengwu/7664719