安卓快速解析xml工具类

xml工具类XmlHelper

利用sax的DefaultHandler做快速的解析,并通过反射把解析结果储存在一个对象里或者一个储存该对象的list里。

XmlHelper代码

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class XmlHelper {
    private static XmlHelper util;

    public static XmlHelper getInstance() {

        if (util == null) {
            util = new XmlHelper();

        }
        return util;

    }

    private XmlHelper() {
        super();
    }

    public  List getList(Class c, String str, String matchesNode) {

        ByteArrayInputStream in = new ByteArrayInputStream(str.getBytes());
        List list = getList(c, in, matchesNode);
        return list;

    }

    public  T getObject(Class c, String str, String matchesNode) {
        ByteArrayInputStream in = new ByteArrayInputStream(str.getBytes());

        T obj = getObject(c, in, matchesNode);
        return obj;

    }

    public  List getList(Class c, InputStream in, String matchesNode) {
        XMLListHandle handle = new XMLListHandle(c, in, matchesNode);
        List list = handle.getList();
        return list;

    }

    public  T getObject(Class c, InputStream in, String matchesNode) {
        XMLListHandle handle = new XMLListHandle(c, in, matchesNode);
        T obj = (T) handle.getObject();
        return obj;

    }

    private class XMLListHandle extends DefaultHandler {
        private T obj;
        private Stack stack = new Stack();
        private Class c = null;
        private Field[] fields = null;
        private List list = new ArrayList();
        private String matchesNode;

        public XMLListHandle(Class c, InputStream in, String matchesNode) {
            this.matchesNode = matchesNode;
            this.c = c;
            this.fields = c.getDeclaredFields();
            try {
                this.obj =  c.newInstance();
            } catch (InstantiationException e1) {
                e1.printStackTrace();
            } catch (IllegalAccessException e1) {
                e1.printStackTrace();
            }
            SAXParserFactory spf = SAXParserFactory.newInstance();
            try {
                SAXParser saxParser = spf.newSAXParser();
                InputSource is = new InputSource(new InputStreamReader(in,
                        "UTF-8"));
                saxParser.parse(is, this);
            } catch (ParserConfigurationException e) {
                e.printStackTrace();
            } catch (SAXException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }

        @Override
        public void startElement(String namespaceURI, String localName,
                String qName, Attributes attr) throws SAXException {
            stack.push(qName);
            try {
                if (qName.equalsIgnoreCase(matchesNode)) {
                    this.obj =  c.newInstance();
                }

            } catch (InstantiationException e1) {
                e1.printStackTrace();
            } catch (IllegalAccessException e1) {
                e1.printStackTrace();
            }

            for (int i = 0; i < attr.getLength(); i++) {
                for (Field field : fields) {
                    String aQName = attr.getQName(i);
                    String aLName = attr.getLocalName(i);
                    String fName = field.getName();
                    if (fName.equalsIgnoreCase(aQName)
                            || fName.equalsIgnoreCase(aLName)) {
                        try {
                            field.setAccessible(true);
                            if (obj != null) {
                                field.set(obj, attr.getValue(aQName));
                            }

                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }
            }

        }

        @Override
        public void characters(char[] ch, int start, int end)
                throws SAXException {
            String tag = stack.peek();
            for (Field field : fields) {
                if (tag.equalsIgnoreCase(field.getName())) {
                    String string = new String(ch, start, end);
                    try {
                        field.setAccessible(true);
                        if (obj != null) {
                            field.set(obj, string);
                        }

                    } catch (IllegalArgumentException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } catch (IllegalAccessException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }

                }
            }

        }

        @Override
        public void endElement(String uri, String localName, String name)
                throws SAXException {
            String tag = stack.peek();
            if (tag.equalsIgnoreCase(matchesNode) && obj != null) {
                list.add(obj);
            }
            stack.pop();

        }

        public List getList() {
            return list;
        }

        public T getObject() {
            return obj;

        }

    }

}

使用方法

String result = " ";
        Bean bean = XmlHelper.getInstance().getObject(Bean.class, result,
                "root");

        List list = XmlHelper.getInstance().getList(Bean.class, result,
                "obj");

        System.out.println(bean.toString());

        for (Bean bean2 : list) {
            System.out.println(bean2.toString());
        }

Bean.java

public class Bean {

    String name="";
    String age="";
    @Override
    public String toString() {
        return "Bean [name=" + name + ", age=" + age + "]";
    }



}

打印结果

03-10 13:25:01.571: I/System.out(21465): Bean [name=name1, age=20]
03-10 13:25:01.571: I/System.out(21465): Bean [name=name, age=10]
03-10 13:25:01.571: I/System.out(21465): Bean [name=name1, age=20]

你可能感兴趣的:(android工具类)