首页 | 新闻 | 新品 | 文库 | 方案 | 视频 | 下载 | 商城 | 开发板 | 数据中心 | 座谈新版 | 培训 | 工具 | 博客 | 论坛 | 百科 | GEC | 活动 | 主题月 | 电子展
返回列表 回复 发帖

Android-使用Dom对XML进行增删改查 更新

Android-使用Dom对XML进行增删改查 更新

4. 查找

    自定义一个稍微简单的XML:

    <?xml version="1.0" encoding="utf-8" standalone='yes'?>
    <packages key1="value1" key2="value2">
        <last-platform-version internal="22" external="22" fingerprint="honeybot/H1f/H1f:5.1.1/LMY47V/huiyu01091530:userdebug/test-keys"/>
        <database-version internal="3" external="3"/>
        <permission-trees/>
        <permissions>
            <item name="android.permission.SET_SCREEN_COMPATIBILITY" package="android" protection="2"/>
            <item name="android.permission.MEDIA_CONTENT_CONTROL" package="android" protection="18"/>
            <item name="android.permission.DELETE_PACKAGES" package="android" protection="18"/>
            <item name="com.android.voicemail.permission.ADD_VOICEMAIL" package="android" protection="1"/>
        </permissions>
        <package name="com.android.providers.downloads" codePath="/system/priv-app/DownloadProvider" nativeLibraryPath="/system/priv-app/DownloadProvider/lib" flags="1074282053" ft="15f9e785498" it="15f9e785498" ut="15f9e785498" version="22" sharedUserId="10002">
            <sigs count="1">
                <cert index="1"/>
            </sigs>
            <proper-signing-keyset identifier="2"/>
            <signing-keyset identifier="2"/>
        </package>
    </packages>

    使用上面的代码去解析,Document如下:

    1876233-3607cf5d51282eae.png
    1.png

    Documnet,it is the root of the document tree, and provides the primary access to the document's data. 就是整个xml的root,通过它可以获取到xml的相关信息。

    xmlVersion,代表的是xml的版本

    children,子节点,是Element,对应上面的,是最外层的package

    Element,是xml的最外层的结点,由document.getDocumentElement() 得到 。

    Node,结点。何为结点?其实就是一个<abc></abc>的一个结点信息,存储着一些,结点本身的属性,和其结点下的子结点等等。

    //得到最外层的节点
    Element element = document.getDocumentElement();
    //得到节点的属性
    NamedNodeMap namedNodeMap = element.getAttributes();
    //便利属性并log输出
    for (int i = 0; i < namedNodeMap.getLength(); i++) {
        Node node = namedNodeMap.item(i);
        node.getNodeName();//key
        node.getTextContent();//value
        node.getNodeType();//node type
    }
    //得到子节点列表
    NodeList nodeList = element.getChildNodes();
    for (int i = 0; i < nodeList.getLength(); i++) {
        Node node = nodeList.item(i);
        //每个node下面,也可能有node,和,node的属性,获取都如上所示
    }

    Node,每个node里面的属性,也是node,每个node下的子节点node也是一个一个node

      //节点的属性
       Node node = namedNodeMap.item(i);
       node.getNodeName();//key
       node.getTextContent();//value
       node.getNodeType();//node type
      //节点下的子节点
      NodeList nodeList = element.getChildNodes();
      for (int i = 0; i < nodeList.getLength(); i++) {
         Node node = nodeList.item(i);
         //每个node下面,也可能有node,和,node的属性,获取都如上所示
      }

    4.1 实践一下,查找

    在android系统里面,安装的每一个app,其信息都被存到一个xml里面:/data/system/packages.xml,可以通过root去查看里面的内容,大概如下(其实上面的例子就是从这个xml文件copy来的):

    <?xml version="1.0" encoding="utf-8" standalone='yes'?>
    <packages key1="value1" key2="value2">
        <last-platform-version internal="22" external="22" fingerprint="honeybot/H1f/H1f:5.1.1/LMY47V/huiyu01091530:userdebug/test-keys"/>
        <database-version internal="3" external="3"/>
        <permission-trees/>
        <permissions>
            //一堆的权限
            <item name="android.permission.SET_SCREEN_COMPATIBILITY" package="android" protection="2"/>
        </permissions>
        //一堆的app
        <package name="com.android.providers.downloads" codePath="/system/priv-app/DownloadProvider" nativeLibraryPath="/system/priv-app/DownloadProvider/lib" flags="1074282053" ft="15f9e785498" it="15f9e785498" ut="15f9e785498" version="22" sharedUserId="10002">
            <sigs count="1">
                <cert index="1"/>
            </sigs>
            <proper-signing-keyset identifier="2"/>
            <signing-keyset identifier="2"/>
        </package>
    </packages>

而现在有个需求,查找是否有app:com.xx.xx,也就是查找xml中的package节点中的name属性值有没有此包名。
我们先封装一下代码吧:

    public class XmlUtils {
     
        /**
         * DOM解析
         *  把文档中的所有元素,按照其出现的层次关系,解析成一个个Node对象(节点)。
         *  缺点是消耗大量的内存。
         * @param xmlFilePath   文件
         * @return  Document
         */
        public static Document loadWithDom(String xmlFilePath) {
            try {
                File file = new File(xmlFilePath);
                if (!file.exists()) {
                    throw new RuntimeException("not find file:" + xmlFilePath);
                }
                else {
                    InputStream inputStream = new FileInputStream(file);
                    DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
                    DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
                    Document document = documentBuilder.parse(inputStream);
                    try {
                        inputStream.close();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    return document;
                }
            } catch (ParserConfigurationException | IOException | SAXException e) {
                return null;
            }
        }
     
        public static Observable<Document> loadWithDomRx(String xmlFilePath) {
            return Observable.just(loadWithDom(xmlFilePath));
        }
    }
返回列表