使用DOM解析Android XML布局文件 [英] Using DOM to parse an Android XML layout file

查看:81
本文介绍了使用DOM解析Android XML布局文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始使用DOM,想解析一个Android XML布局文件 例如

I've just started using DOM and wanted to parse an Android XML layout file e.g.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" 
android:padding="30dip">

<LinearLayout
    android:orientation="vertical"
    android:layout_height="wrap_content"
    android:layout_width="fill_parent"
    android:layout_gravity="center" >


    <EditText android:id="@+id/entry1" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:background="@android:drawable/editbox_background"/>

    <EditText android:id="@+id/entry2" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:background="@android:drawable/editbox_background"/>


    <EditText android:id="@+id/entry3" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:background="@android:drawable/editbox_background"/>

</LinearLayout>
</LinearLayout>

并获得这样的输出(仅在EditText的ID上):

And get output like this (only on the ID's of the EditText's):

ID: @+id/entry1
ID: @+id/entry2
ID: @+id/entry3

有没有办法做到这一点?有人可以给我指出一个教程或给我一些想法吗?

Is there a way to do this? Could someone please point me to a tutorial or give me some ideas?

当前代码:

final String ANDROID_ID = "android:id";

            try {
                File fXmlFile = new File("res/layout/page1.xml");
                DocumentBuilderFactory dbFactory = DocumentBuilderFactory
                        .newInstance();
                DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
                Document doc = dBuilder.parse(fXmlFile);
                doc.getDocumentElement().normalize();

                NodeList nList = doc.getElementsByTagName("Button");

                for (int temp = 0; temp < nList.getLength(); temp++) {
                    Node nNode = nList.item(temp);
                    if (nNode.getNodeType() == Node.ELEMENT_NODE) {
                        Element eElement = (Element) nNode;

                        if (eElement.hasAttribute(ANDROID_ID))
                            System.out.println("ID: "
                                    + eElement.getAttribute(ANDROID_ID));
                    }
                }
            } 

            catch (Exception e) {
                System.out.println("Catch");


  e.printStackTrace();
        }

谢谢!

推荐答案

如果要使用Java DOM,请尝试执行类似的操作

If you want to use Java DOM, try doing something like this

    final String ANDROID_ID = "android:id";

    try {

        File fXmlFile = new File("res/layout/parse.xml");
        DocumentBuilderFactory dbFactory = DocumentBuilderFactory
                .newInstance();
        DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
        Document doc = dBuilder.parse(fXmlFile);
        doc.getDocumentElement().normalize();

        NodeList nList = doc.getElementsByTagName("*");

        for (int temp = 0; temp < nList.getLength(); temp++) {
            Node nNode = nList.item(temp);
            if (nNode.getNodeType() == Node.ELEMENT_NODE) {
                Element eElement = (Element) nNode;

                if (eElement.hasAttribute(ANDROID_ID))
                    System.out.println("ID: "
                            + eElement.getAttribute(ANDROID_ID));
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

执行此操作的方法可能更有效(例如,使用SAX),但是它可以工作.

There are probably much more efficient ways to do this (like using SAX instead), but it works.

这篇关于使用DOM解析Android XML布局文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆