标签:
参考:http://blog.csdn.net/liuhe688/article/details/6415593
首先定义一个接口
package com.lei.xml; import java.io.InputStream; import java.util.List; public interface XMLParser { // 解析xml <T> List<T> xmlParse(InputStream is); //构造xml <T> String xmlSerializer(List<T> list); }
1. PULL解析
解析速度快,事件处理,不会预加载整个文档
package com.lei.xml; import java.io.IOException; import java.io.InputStream; import java.io.StringWriter; import java.util.ArrayList; import java.util.List; import org.xmlpull.v1.XmlPullParser; import org.xmlpull.v1.XmlPullParserException; import org.xmlpull.v1.XmlSerializer; import android.util.Xml; public class PullParse implements XMLParser { public static PullParse getInstance() { return new PullParse(); } public <T> List<T> xmlParse(InputStream is) { List<Book> list = null; Book book = null; XmlPullParser parser = Xml.newPullParser(); // XmlPullParser parser = // XmlPullParserFactory.newInstance().newPullParser(); try { parser.setInput(is, "utf-8"); int type = parser.getEventType(); while (type != XmlPullParser.END_DOCUMENT) {//整个xml结束标签,出口 switch (type) { case XmlPullParser.START_DOCUMENT://整个xml开始标签,入口 list = new ArrayList<Book>(); break; case XmlPullParser.START_TAG://子开始标签 if (parser.getName().equals("book")) { book = new Book(); } else if (parser.getName().equals("id")) { type = parser.next(); book.setId(Integer.parseInt(parser.getText())); } else if (parser.getName().equals("name")) { type = parser.next(); book.setName(parser.getText()); } break; case XmlPullParser.END_TAG://子结束标签 if (parser.getName().equals("book")) { list.add(book); } break; } type = parser.next(); } } catch (XmlPullParserException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return (List<T>) list; } public <T> String xmlSerializer(List<T> list) { List<Book> lists = (List<Book>) list; XmlSerializer serializer = Xml.newSerializer(); try { serializer.startDocument("utf-8", true); serializer.setOutput(new StringWriter()); serializer.startTag("", "books"); for (Book book : lists) { serializer.startTag("", "book"); serializer.attribute("", "id", String.valueOf(book.getId())); serializer.startTag("", "name"); serializer.text(book.getName()); serializer.endTag("", "name"); serializer.endTag("", "book"); } serializer.endTag("", "books"); serializer.endDocument(); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (IllegalStateException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return serializer.toString(); } }
树形结构,速度款,占用内存少,会预加载整个文档,适用于文档较小。
package com.lei.xml; import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; import java.util.List; 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 com.lei.utils.Logs; public class SaxParse implements XMLParser { public static SaxParse getInstance() { return new SaxParse(); } /** * 解析xml */ public <T> List<T> xmlParse(InputStream is) { try { SAXParser parser = SAXParserFactory.newInstance().newSAXParser(); MyXmlHandler handler = new MyXmlHandler(); parser.parse(is, handler); return (List<T>) handler.getBookList(); } catch (ParserConfigurationException e) { e.printStackTrace(); } catch (SAXException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return null; } /** * 生成xml */ public <T> String xmlSerializer(List<T> list) { return null; } class MyXmlHandler extends DefaultHandler { List<Book> list; Book book; StringBuilder builder; public List<Book> getBookList() { return list; } // 入口,开始 @Override public void startDocument() throws SAXException { super.startDocument(); Logs.v("startDocument"); list = new ArrayList<Book>(); builder = new StringBuilder(); } // 出口,结束 @Override public void endDocument() throws SAXException { super.endDocument(); Logs.v("endDocument"); } // 开始解析开始标签 @Override public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { super.startElement(uri, localName, qName, attributes); Logs.v("startElement " + "uri " + uri + " localName " + localName + " qName " + qName); if ("book".equals(localName)) { book = new Book(); } builder.setLength(0);// 每次豆浆builder清空 } // 结束标签 @Override public void endElement(String uri, String localName, String qName) throws SAXException { super.endElement(uri, localName, qName); Logs.v("endElement " + "uri " + uri + " localName " + localName + " qName " + qName); if ("id".equals(localName)) { book.setId(Integer.parseInt(builder.toString())); } else if ("name".equals(localName)) { book.setName(builder.toString()); } else if ("book".equals(localName)) { list.add(book); } } // 获取子标签的内容 @Override public void characters(char[] ch, int start, int length) throws SAXException { super.characters(ch, start, length); builder.append(ch, start, length); Logs.v("characters " + new String(ch, start, length)); } } }
预先加载整个文档,占用内存大,不推荐使用
package com.lei.xml; import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; import java.util.List; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import org.xml.sax.SAXException; public class DomParse implements XMLParser { public static DomParse getInstance(){ return new DomParse(); } public <T> List<T> xmlParse(InputStream is) { List<Book> list = new ArrayList<Book>(); DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); try { DocumentBuilder builder = factory.newDocumentBuilder(); Document doc = builder.parse(is); Element element = doc.getElementById("book"); NodeList nodeList = element.getChildNodes(); Book book = null; for (int i = 0; i < nodeList.getLength(); i++) { Node node = nodeList.item(i); book = new Book(); NodeList details = node.getChildNodes(); for (int j = 0; j < details.getLength(); j++) { if (node.getLocalName().equals("id")) { book.setId(Integer.parseInt(node.getNodeValue())); } else if (node.getLocalName().equals("name")) { book.setName(node.getNodeValue()); } } list.add(book); } } catch (ParserConfigurationException e) { e.printStackTrace(); } catch (SAXException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return (List<T>) list; } public <T> String xmlSerializer(List<T> list) { return null; } }
在 assets下放了一个books.xml
private void parseXmBySax() { try { InputStream is = getAssets().open("books.xml"); List<Book> list = new SaxParse().xmlParse(is); for (Book book : list) { mShowTv.append(book.toString()); } } catch (IOException e) { e.printStackTrace(); } } private void parseXmByPull() { InputStream is = null; try { is = getAssets().open("books.xml"); } catch (IOException e) { e.printStackTrace(); } List<Book> list = PullParse.getInstance().xmlParse(is); for (Book book : list) { mShowTv.append(book.toString()); } } private void parseXmlByDom() { InputStream is = null; try { is = getAssets().open("books.xml"); } catch (IOException e) { e.printStackTrace(); } List<Book> list = new SaxParse().xmlParse(is); for (Book book : list) { mShowTv.append(book.toString()); } }
标签:
原文地址:http://blog.csdn.net/qq_31116683/article/details/51332366