XmlPullParser 解析xml文件

实例 

<permissionlist>
    <package name="com.coolpad.coolclean">
        <perm name="android.permission.READ_PHONE_STATE" />
        <perm name="android.permission.WRITE_EXTERNAL_STORAGE" />
        <perm name="android.permission.READ_EXTERNAL_STORAGE" />
    </package>
    <package name="com.yulong.coolfuncservice">
        <perm name="android.permission.READ_PHONE_STATE" />
        <perm name="android.permission.WRITE_EXTERNAL_STORAGE" />
    </package>
    <package name="com.yulong.membercenter">
        <perm name="android.permission.READ_PHONE_STATE" />
    </package>
    <package name="com.yulong.android.gallery">
        <perm name="android.permission.READ_EXTERNAL_STORAGE" />
        <perm name="android.permission.WRITE_EXTERNAL_STORAGE" />
    </package>
</permissionlist>

上面的xml 文件,< xxx   > xxx </xxx > ; <xxx  />  这个格式的都属于 TAG, 即用 xmlPullParser.getEventType()  和 parser.next() 函数判断

java 解析

private void doReadXml() {
    Log.d(TAG,"phg__ doReadXml");
    AtomicFile whiteListFile = new AtomicFile(new File(NEWPATH, WHITEFILE));
    XmlPullParser xmlPullParser = Xml.newPullParser();
    InputStream infile = null;

    try {
        infile = whiteListFile.openRead();
        xmlPullParser.setInput(infile, StandardCharsets.UTF_8.name());
        int type = xmlPullParser.getEventType();
        while (type != XmlPullParser.END_DOCUMENT) {
            switch (type) {
                case XmlPullParser.START_TAG:  //  start tag
                    switch (xmlPullParser.getName()) {
                        case TAG_WHITELIST:
                            readWhiteListXml(xmlPullParser);
                            break;
                        case TAG_PERMISSIONS:
                            readPkgXml(xmlPullParser);  // 解析第二段
                            break;
                        default:
                            break;
                    }
            }
            // Circular resolution
            type = xmlPullParser.next();
        }
    } catch (Exception e) {
        Log.e(TAG, "phg__  Unable to read notification policy" + e);
    } finally {
        IoUtils.closeQuietly(infile); // After finished read, delete the atomic file
    }
}

private void readPkgXml(XmlPullParser parser)
        throws IOException, XmlPullParserException {
    final int outerDepth = parser.getDepth();
    int type;

    while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
            && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
        if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
            if (TAG_PERMISSIONS.equals(parser.getName()))
                return;
            continue;
        }

        switch (parser.getName()) {
            case TAG_PACKAGE:
                String pkgName = parser.getAttributeValue(0);
                Log.d(TAG,">>> pkgName =" + pkgName);  // 打印log
                List<String> perms = getPermissions(pkgName);
                readPermsXml(parser, perms,TAG_PACKAGE,TAG_PERMS);
                break;
        }
    }
}

private void readPermsXml(XmlPullParser parser, List<String> perms, final String tag1, final String tag2)
        throws IOException, XmlPullParserException {
    final int outerDepth = parser.getDepth();
    int type;
    while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
            && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
        if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
            String tmp = parser.getName();
            Log.d(TAG,">>> tmp " + tmp);
            if (tag1.equals(tmp))
                return;
            continue;
        }

        String tmp2 = parser.getName();
        Log.d(TAG,">>> tmp2 = " + tmp2);
        if (tag2.equals(parser.getName())) {
            String permName = parser.getAttributeValue(null, ATTR_NAME);
            Log.d(TGA,">>> permName = " + permName);           
            if (!perms.contains(permName)) {
                perms.add(permName);
            }
            break;
        }
    }
}
>>> pkgName = com.coolpad.coolclean
>>> tmp null  // <package name="com.coolpad.coolclean"> TEXT 后面 获取到null
>>> tm2 = perm 
>>> permName = android.permission.READ_PHONE_STATE
>>> tmp perm
>>> tmp null
>>> tm2 = perm
>>> permName = android.permission.WRITE_EXTERNAL_STORAGE
>>> tmp perm
>>> tmp null
>>> tm2 = perm
>>> permName = android.permission.READ_EXTERNAL_STORAGE
>>> tmp perm
>>> tmp null
>>> pkgName = com.yulong.coolfuncservice
>>> tmp null
>>> tm2 = perm
>>> permName = android.permission.READ_PHONE_STATE
>>> tmp perm
>>> tmp null
>>> tm2 = perm
>>> permName = android.permission.WRITE_EXTERNAL_STORAGE
>>> tmp perm
>>> tmp null
>>> pkgName = com.yulong.membercenter
>>> tmp null
>>> tm2 = perm
>>> permName = android.permission.READ_PHONE_STATE
>>> tmp perm
>>> tmp null

<a> ... </a>这种形式的标签,解析时会读取到两个TEXT事件:一个在<a> (这是START_TAG)之后产生,一个在</a> (这是END_TAG)之后产生
<b />这种形式的标签只会在<b> END_TAG之后读取到一个TEXT事件,<b> START_TAG之后不会产生TEXT事件
 

上一篇:Android技术分享| Android WebRTC 对 AudioRecord 的使用


下一篇:942. 增减字符串匹配