如何使用DOM Android应用程序中正确读取XML? [英] How to use DOM to read XML within Android applications correctly?

查看:194
本文介绍了如何使用DOM Android应用程序中正确读取XML?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我能够得到我想要如果XML文件在我的机器上本地存储的信息,但是当存储在手机上不工作得非常好读它。

我试过XMLPullParser但它提取有关的ID名称等二进制信息,我想实际的名称。

code:

 最后弦乐ANDROID_ID =机器人:ID            尝试{
                文件fXmlFile =新的文件(RES /布局/ page1.xml);
                的DocumentBuilderFactory dbFactory =的DocumentBuilderFactory
                        .newInstance();
                的DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
                文档的DOC = dBuilder.parse(fXmlFile);
                doc.getDocumentElement()正常化()。                节点列表NLIST = doc.getElementsByTagName(按钮);                对于(INT温度= 0;&温度LT; nList.getLength();临时++){
                    节点nNode = nList.item(临时);
                    如果(nNode.getNodeType()== Node.ELEMENT_NODE){
                        元素eElement =(元)nNode;                        如果(eElement.hasAttribute(ANDROID_ID))
                            的System.out.println(ID
                                    + eElement.getAttribute(ANDROID_ID));
                    }
                }
            }            赶上(例外五){
                的System.out.println(捕获);
  e.printStackTrace();
        }


解决方案

在XML解析此的链接介绍好。在这里,你可以找到其他的解析器也与 xmlPullParser

I'm able to get the information I want if the XML file is stored locally on my machine, but reading it when stored on the phone isn't working very well.

I've tried XMLPullParser but it extracts binary information about the id names etc and I'd like the actual name.

Code:

    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();
        }

解决方案

Over XML parsing this link describes well. Here you can find other parsers too with xmlPullParser.

这篇关于如何使用DOM Android应用程序中正确读取XML?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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