1. PULL解析
解析速度快,事件处理,不会预加载整个文档
[java] view plain copy
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
List
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
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
}
public
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();
}
}
2. SAX解析
树形结构,速度快,占用内存少,会预加载整个文档,适用于文档较小。
[java] view plain copy
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
try {
SAXParser parser = SAXParserFactory.newInstance().newSAXParser();
MyXmlHandler handler = new MyXmlHandler();
parser.parse(is, handler);
return (List
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
/**
* 生成xml
*/
public
return null;
}
class MyXmlHandler extends DefaultHandler {
List
Book book;
StringBuilder builder;
public List
return list;
}
// 入口,开始
@Override
public void startDocument() throws SAXException {
super.startDocument();
Logs.v("startDocument");
list = new ArrayList
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));
}
}
}
3.DOM解析
预先加载整个文档,占用内存大,不推荐使用
[java] view plain copy
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
List
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
}
public
return null;
}
}
使用:
在 assets下放了一个books.xml
[java] view plain copy
private void parseXmBySax() {
try {
InputStream is = getAssets().open("books.xml");
List
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
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
for (Book book : list) {
mShowTv.append(book.toString());
}
}