在Android中有几种xml解析方式,下面我说其中一种,在Android平台上可以使用Simple API for XML(SAX):

我的资源文件:value.xml

<?xml version="1.0" encoding="UTF-8"?><xml><PersonModule="1"><name>王二</name><age>18</age></Person><PersonModule="2"><name>张三</name><age>20</age></Person><PersonModule="3"><name>李四</name><age>22</age></Person></xml>

我的程序:

import org.xml.sax.Attributes;import org.xml.sax.SAXException;import org.xml.sax.helpers.DefaultHandler;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.IOException;import java.io.InputStream;import javax.xml.parsers.ParserConfigurationException;import javax.xml.parsers.SAXParser;import javax.xml.parsers.SAXParserFactory;public class Sax extends DefaultHandler {    private String tag ;//标签    private String tag_value;//存放临时标签    private String name;    private String age;    /**     * 得到到标签元素值     * Packeage Module="fireware", Module 为属性 ,"fireware"为属性值。    <Version>smart.01.sc</Version>  Version 为标签元素,smart.01.sc为元素值。     */    @Override    public void characters(char[] ch, int start, int length) throws SAXException {        // TODO Auto-generated method stub        if (tag != null) {            tag_value = new String(ch, start, length);            if (tag.equals("name")) {                name = tag_value;                System.out.println(" name = " + name);            }            if (tag.equals("age")) {                age = tag_value;                System.out.println(" age = " + age);            }        }    }    /**     * 解析结束执行一次     */    @Override    public void endDocument() throws SAXException {        // TODO Auto-generated method stub        super.endDocument();    }    /**     * 遇到结束标签就会执行     */    @Override    public void endElement(String uri, String localName, String qName) throws SAXException {        // TODO Auto-generated method stub        tag = null;    }    /**     * 开始解析时调用一次     */    @Override    public void startDocument() throws SAXException {        // TODO Auto-generated method stub        super.startDocument();    }    /**     * 遇到开始标签就会执行     * Attributes 中存放着标签属性     */    @Override    public void startElement(String uri, String localName, String qName, Attributes attributes)            throws SAXException {        // TODO Auto-generated method stub        tag = qName;        for (int i = 0; i < attributes.getLength(); i++) {            System.out.println(" attributes.getValue(" + i + ") = " + attributes.getValue(i));        }    }    /**     * @param fileName     */    public void parserXml(String fileName) {        InputStream is = null ;//创建输入流读入XML文件        try {            //创建解析器            SAXParserFactory saxfac = SAXParserFactory.newInstance();            SAXParser saxparser = saxfac.newSAXParser();            saxparser.setProperty("http://xml.org/sax/features/namespaces", true);//设置命名空间            System.out.println(fileName);            is = new FileInputStream(fileName);            //解析            saxparser.parse(is, new Sax());        } catch (ParserConfigurationException e) {            e.printStackTrace();        } catch (SAXException e) {            e.printStackTrace();        } catch (FileNotFoundException e) {            e.printStackTrace();        } catch (IOException e) {            e.printStackTrace();        }finally{            if(is!=null){                try {                    is.close();                } catch (IOException e) {                    // TODO Auto-generated catch block                    e.printStackTrace();                }            }        }    }    public static void main(String [] str){        new Sax().parserXml("res/value.xml");    }}

执行后打印:

res/value.xml
attributes.getValue(0) = 1
name = 王二
age = 18
attributes.getValue(0) = 2
name = 张三
age = 20
attributes.getValue(0) = 3
name = 李四
age = 22


更多相关文章

  1. android Property Animation 属性动画详解
  2. 探讨相对布局的重要特性和属性
  3. Android 的系统属性(SystemProperties)设置分析
  4. Android TextView 支持的HTML标签
  5. android 组件,xml布局中属性详解
  6. [置顶] android:configChanges属性

随机推荐

  1. [置顶] android Activity和service深入介
  2. !!!Android开发、测试持续集成环境Jenkins搭
  3. Android应用获取系统属性
  4. Android(安卓)Studio TV开发教程(二)管理电
  5. Android中控制软键盘的方法总结
  6. 安桌HttpURLConnection错误解决
  7. android_lesson_1
  8. Android(安卓)MVP+Retrofit(封装)+RxJava
  9. React Native For Android(安卓)Windows
  10. ANDROID2.3改进