采用Pull完成对xml内容的解析

假设我们的xml文件如下

<?xml version="1.0" encoding="utf-8"?>
<config>
    <chatServerIp>192.168.1.108</chatServerIp>
    <port>5222</port>
    <serviceName>qq.com</serviceName>
</config>

那么我们读取的代码如下:

    private void readConfig() throws Exception {
        Resources res = this.getResources();
        XmlResourceParser parser = res.getXml(R.xml.config);
        int type = parser.getEventType();
        while(type != parser.END_DOCUMENT){
            if(type == parser.START_TAG){
                String name = parser.getName();
                if("chatServerIp".equals(name)){
                    chatServerIp = parser.nextText();
                } else if("port".equals(name)){
                    port = Integer.parseInt(parser.nextText());
                }else if("serviceName".equals(name)){
                    serviceName = parser.nextText();
                }
            }
            type = parser.next();
        }
    }


你可能感兴趣的:(采用Pull完成对xml内容的解析)