Android pull方式解析XML文件
个人认为DOM, SAX, PULL3种解析XML的方式中Pull是最适合在android系统中使用的,轻便,灵活,占用资源较少
废话不说,先把xml源文件描述如下:
package com.example.test;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.IOException;import java.io.InputStream;import java.util.ArrayList;import org.xmlpull.v1.XmlPullParser;import org.xmlpull.v1.XmlPullParserException;import org.xmlpull.v1.XmlPullParserFactory;public class ConfigXMLParser {private InspurConfigBean inspurConfigBean;private ArrayList<SerialPortBean> serialList;private SerialPortBean serialPortBean;private InputStream is;private boolean isNormal = true;private boolean startSerialElement = false;public ConfigXMLParser(String xmlPath){File file = new File(xmlPath);if (!file.exists()){inspurConfigBean = null;isNormal = false;}else{try {is = new FileInputStream(file);} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();inspurConfigBean = null;}}}public void parse() throws XmlPullParserException, IOException {if (!isNormal){inspurConfigBean = null;return;}XmlPullParserFactory factory = null;XmlPullParser parser = null;factory = XmlPullParserFactory.newInstance();parser = factory.newPullParser();parser.setInput(is, "UTF-8");int eventType = parser.getEventType();String qName = "";while(eventType != XmlPullParser.END_DOCUMENT){switch(eventType){case XmlPullParser.START_DOCUMENT:inspurConfigBean = new InspurConfigBean();break;case XmlPullParser.START_TAG:qName = parser.getName();if (qName.equalsIgnoreCase("root")){}else if (qName.equalsIgnoreCase("anim")){}else if (qName.equalsIgnoreCase("switcher")){if (parser.nextText().equalsIgnoreCase("yes")){inspurConfigBean.setSwitcher(true);}else{inspurConfigBean.setSwitcher(false);}}else if (qName.equalsIgnoreCase("server")){}else if (qName.equalsIgnoreCase("ip")){inspurConfigBean.setServer_ip(parser.nextText().toString().trim());}else if (qName.equalsIgnoreCase("port")){inspurConfigBean.setServer_port(parser.nextText().toString().trim());}else if (qName.equalsIgnoreCase("serial")){serialList = new ArrayList<SerialPortBean>();startSerialElement = true;}if (startSerialElement){if (qName.equalsIgnoreCase("com")){serialPortBean = new SerialPortBean();}else if (qName.equalsIgnoreCase("identifier")){serialPortBean.setIdentifier(parser.nextText().toString().trim());}else if (qName.equalsIgnoreCase("tty")){serialPortBean.setTty(parser.nextText().toString().trim());}else if (qName.equalsIgnoreCase("bandrate")){serialPortBean.setBandrate(Integer.parseInt(parser.nextText()));}else if (qName.equalsIgnoreCase("charlength")){serialPortBean.setCharLength(Integer.parseInt(parser.nextText()));}else if (qName.equalsIgnoreCase("timeout")){serialPortBean.setTimeout(Integer.parseInt(parser.nextText()));}}break;case XmlPullParser.END_TAG:qName = parser.getName();if (qName.equalsIgnoreCase("com")){serialList.add(serialPortBean);}else if (qName.equalsIgnoreCase("serial")){inspurConfigBean.setSerialList(serialList);}break;}eventType = parser.next();}}public InspurConfigBean getResult(){return inspurConfigBean;}}