转载请注明出处:http://www.drovik.com/html/0974355211.html 谢谢!
Android平台dom、sax、pull三种解析xml文件的实例代码
效果图:
关键代码:
private List<River> fetchRiverFormXmlByPull(String fileName) {
long startTime = System.currentTimeMillis();
List<River> rivers = new ArrayList<River>();
River river = null;
InputStream inputStream = null;
XmlPullParser xmlPullParser = Xml.newPullParser();
try {
inputStream = getAssets().open(fileName);
xmlPullParser.setInput(inputStream, "utf-8");
xmlPullParser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
//xmlPullParser.nextTag();
int eventType = xmlPullParser.getEventType();
while(eventType != XmlPullParser.END_DOCUMENT) {
switch(eventType) {
case XmlPullParser.START_TAG:
String tag = xmlPullParser.getName();
if(tag.equalsIgnoreCase(riverStr)) {
river = new River();
river.setName(xmlPullParser.getAttributeValue(null, nameStr));
river.setLength(Integer.parseInt(xmlPullParser.getAttributeValue(null, lengthStr)));
}else if(river != null) {
if(tag.equalsIgnoreCase(introductionStr)) {
river.setIntroduction(xmlPullParser.nextText());
} else if(tag.equalsIgnoreCase(imageurlStr)) {
river.setImageurl(xmlPullParser.nextText());
}
}
break;
case XmlPullParser.END_TAG:
if(xmlPullParser.getName().equalsIgnoreCase(riverStr) && river != null) {
rivers.add(river);
river = null;
}
break;
default:
break;
}
eventType = xmlPullParser.next();
}
} catch (XmlPullParserException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Log.d(T, "exec fetchRiverFormXmlByPull use time = " + (System.currentTimeMillis() - startTime));
return rivers;
}
private List<River> fetchRiverFormXmlBySAX(String fileName) {
long startTime = System.currentTimeMillis();
List<River> rivers = null;
SAXParserFactory factory = SAXParserFactory.newInstance();
InputStream inputStream = null;
try {
SAXParser parser = factory.newSAXParser();
XMLReader reader = parser.getXMLReader();
MySaxHandler handler = new MySaxHandler();
reader.setContentHandler(handler);
inputStream = getAssets().open(fileName);
reader.parse(new InputSource(inputStream));
rivers = handler.getRivers();
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Log.d(T, "exec fetchRiverFormXmlBySAX use time = " + (System.currentTimeMillis() - startTime));
return rivers;
}
private class MySaxHandler extends DefaultHandler {
private List<River> rivers = null;
private boolean isRiver = false;
private boolean isIntrocduce = false;
private boolean isImageUrl = false;
private River river = null;
private String TAG = "MySaxHandler";
public MySaxHandler() {
rivers = new ArrayList<River>();
}
@Override
public void startDocument() throws SAXException {
super.startDocument();
Log.d(TAG, "### startDocument");
}
@Override
public void endDocument() throws SAXException {
super.endDocument();
Log.d(TAG, "### endDocument");
}
@Override
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
super.startElement(uri, localName, qName, attributes);
String tagName = localName.length()>0?localName:qName;
if(tagName.equals(riverStr)) {
isRiver = true;
river = new River();
river.setName(attributes.getValue(nameStr));
river.setLength(Integer.parseInt(attributes.getValue(lengthStr)));
}
if(isRiver) {
if(tagName.equals(introductionStr)) {
isIntrocduce = true;
} else if(tagName.equals(imageurlStr)) {
isImageUrl = true;
}
}
}
@Override
public void endElement(String uri, String localName, String qName)
throws SAXException {
super.endElement(uri, localName, qName);
//Log.d(TAG, "### endElement uri=" + uri + " localName=" + localName + " qName=" + qName);
String tagName=localName.length()!=0?localName:qName;
if(tagName.equals(riverStr)) {
isRiver = false;
rivers.add(river);
}
if(isRiver) {
if(tagName.equals(introductionStr)) {
isIntrocduce = false;
} else if(tagName.equals(imageurlStr)) {
isImageUrl = false;
}
}
}
@Override
public void characters(char[] ch, int start, int length)
throws SAXException {
super.characters(ch, start, length);
if(isIntrocduce) {
river.setIntroduction(river.getIntroduction() == null ? "" : river.getIntroduction() + new String(ch,start,length));
}else if(isImageUrl) {
river.setImageurl(river.getImageurl()==null ? "" : river.getImageurl()+ new String(ch,start,length));
}
}
public List<River> getRivers(){
return rivers;
}
}
private List<River> fetchRiverFromXmlByDom(String fileName) {
long startTime = System.currentTimeMillis();
List<River> rivers = new ArrayList<River>();
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
InputStream inputStream = null;
try {
DocumentBuilder builder = factory.newDocumentBuilder();
inputStream = getAssets().open(fileName);
Document document = builder.parse(inputStream);
Element root = document.getDocumentElement();
NodeList nodeList = root.getElementsByTagName(riverStr);
int noteListSize = nodeList.getLength();
River river = null;
for(int i=0;i<noteListSize;i++) {
river = new River();
Element element = (Element) nodeList.item(i);
river.setName(element.getAttribute(nameStr));
river.setLength(Integer.parseInt(element.getAttribute(lengthStr)));
Element introTag = (Element) element.getElementsByTagName(introductionStr).item(0);
river.setIntroduction(introTag.getFirstChild().getNodeValue());
Element imageUrlTag = (Element) element.getElementsByTagName(imageurlStr).item(0);
river.setImageurl(imageUrlTag.getFirstChild().getNodeValue());
rivers.add(river);
}
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
}
Log.d(T, "exec fetchRiverFromXmlByDom use time = " + (System.currentTimeMillis() - startTime));
return rivers;
}
代码下载地址:http://www.drovik.com/android/demo/PullXmlDemo.rar
- 相关文章
- Android NDK 入门 (209人浏览)
- Android平台的录音放音实例代码 (1501人浏览)
- android语音开发中利用Speex 回音消除模块 (883人浏览)
- 解决linux平台开发Android应用程序时adb devices显示no p (87人浏览)
- Android中利用AudioRecord和AudioTrack采取和播放音频 (1969人浏览)
- 【转】修改android开机画面 (131人浏览)
- 关于Android锁屏的问题 (261人浏览)
- 1楼 cartier knock off watches ladies 发表于 2015-3-30 19:22:27
- Oh yeah, mình làm ?c r??, hhaha cartier knock off watches ladies http://bbs.xkd.cn/forum.php?mod=viewthread&tid=78780&fromuid=5694