现在的位置: 首页 > 综合 > 正文

Android DOM解析xml

2014年06月03日 ⁄ 综合 ⁄ 共 3169字 ⁄ 字号 评论关闭

上一篇文章中记录了采用PULL方式读写xml文件(Android读写xml),本文将采用DOM方式解析xml

DOM方式解析xml是先把xml文档都读到内存中,然后再用DOM API来访问树形结构,并获取数据的,但是这样一来,如果xml文件很大呢?手机CPU处理能力当然不能与PC机器比,因此在处理效率方面就相对差了,当然这是对于其他方式处理xml文档而言。

person.xml文件放在项目的assets目录,内容如下:

<?xml version="1.0" encoding="utf-8"?>
<persons>
    <person name="西门吹雪" age="30">
        <profession>
            外号剑神
        </profession>
        <introduction>
            西门吹雪,是古龙的武侠小说《陆小凤传奇》中的人物之一。他擅长用剑,剑法超绝。喜穿白衣,面容冷峻。生性冷僻,朋友极少。他始终以剑术为生命的最高追求,书中大多时候给人无情、冷漠的感觉。
        </introduction>

    </person>

    <person name="令狐冲" length="25">
        <profession>
            吸星大法,易筋经,五岳剑法,冲灵剑法,独孤九剑(成名绝技)
        </profession>
        <introduction>
            豁达、开朗。率性而为,放浪潇洒,豪放不羁,重情重义,心高气傲,不惧强横,有“交友当如令狐冲”之说。
        </introduction>

    </person>

    <person name="扫地僧" length="80">
        <profession>
            萧远山和慕容博入少林时已是当时的一流高手,而扫地僧当时已能在旁窥视他们的一举一动而萧远山毫无知觉,可见扫地僧当时武功已远在他二人之上。
        </profession>
        <introduction>
            扫地僧又被称作无名老僧,被很多金庸迷认为是金庸小说中的第一高手,于《天龙八部》第四十三章登场。关于无名老僧的身份姓名等,无可查询,皆是不详,一个出场极少的角色,倏然而出,倏然又隐,但又让人印象深刻。
        </introduction>
    </person>
</persons>

主要读取函数如下:

public List<Person> getPersonsFromXml(String fileName) {
        List<Person> persons = new ArrayList<Person>();
        DocumentBuilderFactory factory = null;
        DocumentBuilder builder = null;
        Document document = null;
        InputStream inputStream = null;
        //首先找到xml文件
        factory = DocumentBuilderFactory.newInstance();
        try {
            //找到xml,并加载文档
            builder = factory.newDocumentBuilder();
            inputStream = this.getResources().getAssets().open(fileName);
            document = builder.parse(inputStream);
            //找到根Element
            Element root = document.getDocumentElement();
            NodeList nodes = root.getElementsByTagName("person");
            log(" new length : " + nodes.getLength());
            //遍历根节点所有子节点,persons 下所有person
            Person person = null;
            for (int i = 0; i < nodes.getLength(); i++) {
                person = new Person();
                //获取person元素节点
                Element personElement = (Element) (nodes.item(i));
                //获取person中name属性值
                person.setName(personElement.getAttribute("name"));
                person.setAge(Integer.parseInt(personElement.getAttribute("age")));
                //获取person下profession和introduction标签
                Element profession = (Element) personElement.getElementsByTagName("profession").item(0);
                person.setProfession(profession.getFirstChild().getNodeValue());
                Element introduction = (Element) personElement.getElementsByTagName("introduction").item(0);
                person.setIntroduction(introduction.getFirstChild().getNodeValue());

                persons.add(person);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (SAXException e) {
            e.printStackTrace();
        } catch (ParserConfigurationException e) {
            e.printStackTrace();
        } finally {
            try {
                inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return persons;
    }

Person类如下:

public class Person implements Serializable {
        private static final long serialVersionUID = 1L;
        private String name;
        private int age;
        private String introduction;
        private String profession;

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public int getAge() {
            return age;
        }

        public void setAge(int age) {
            this.age = age;
        }

        public String getIntroduction() {
            return introduction;
        }

        public void setIntroduction(String introduction) {
            this.introduction = introduction;
        }

        public String getProfession() {
            return profession;
        }

        public void setProfession(String profession) {
            this.profession = profession;
        }


    }

由于person.xml放在assets目录下,因此使用时可以直接调用函数:

getPersonsFromXml("person.xml");

其实 ,DOM方式解析xml的思路大致为:

首先利用DocumentBuilderFactory创建一个DocumentBuilderFactory实例

然后利用DocumentBuilderFactory创建DocumentBuilder

然后加载XML文档(Document)

然后获取文档的根结点(Element),

然后获取根结点中所有子节点的列表(NodeList)

然后使用再获取子节点列表中的需要读取的结点。

抱歉!评论已关闭.