Take the 2-minute tour ×

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.


    final String ANDROID_ID = "android:id";

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

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

                    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) {

share|improve this question

2 Answers 2

in the XmlPullParser documentation there is a getAttributeCount() and getAttributeByName(int index) that might be useful. You must use it in START_TAG

share|improve this answer
I tried to use the methods you proposed. The id value however comes out as a number when I was hoping it would come out as something similar to '@+id/page1'. I edited my first post with my new code and output. –  Neeta Nov 20 '11 at 17:28
It's probably because it's a "binary" XML file, meaning it's been precompiled for efficiency. I think you want to read the plain XML instead. –  dmon Nov 20 '11 at 17:37
It will be more complicated since you will have to instantiate your own XmlPullParser, but you can probably get the raw xml InputStream using res.openRawResource(R.layout.the_id). –  dmon Nov 20 '11 at 17:41
@dmon I wrote another method earlier which used DOM to read the XML file but it wasn't able to get the XML resource (although it could if it was stored locally on my machine). I think it could be to do with not referencing the resource properly or the code? I have edited the code above with this other DOM method. –  Neeta Nov 20 '11 at 17:49
I've never done this so it was just a hunch. Instead of referencing the file like that, try using the res.openRawResource() method that I suggested and pass that InputStream it into the DocumentBuilder. –  dmon Nov 20 '11 at 17:53

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

share|improve this answer

Your Answer


