David_Garcia
David_Garcia

Reputation: 654

reading Xml file on android

I'm trying to read a Xml file with 120 items like this.

<?xml version="1.0" encoding="utf-8"?>
 <books>
        <Book>
            <bookName >Libro 1</bookName>
            <bookSection>Unidad 1</bookSection>
            <memorization>[A long string of information]…</memorization>
        </Book>
        <Book>.....</Book>
 </books>

In my android app I want to put all this information in a ArrayList<book>

So in the void OnCreate I do this:

Resources res = getResources();
XmlResourceParser x = res.getXml(R.xml.textos);
        try {
            insertXML(x);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (XmlPullParserException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

and InsertXML look like this.

private void insertXML(XmlPullParser x) throws XmlPullParserException, IOException {        
        try {

            int eventType = x.getEventType();

            while (eventType != XmlPullParser.END_DOCUMENT) {

                Textos seleccion = new Textos();

                if ( eventType == XmlPullParser.START_TAG ) {
                    if (x.getAttributeValue(null, "Book") != null) {
                        seleccion.setBook(x.getAttributeValue(null, "bookName"));
                        seleccion.setSection(x.getAttributeValue(null, "bookSection"));
                        seleccion.setMemorization(x.getAttributeValue(null, "memorization"));
                    }

                }

                if ( eventType == XmlPullParser.END_TAG ) {
                    if (x.getName().equals("Book")) {
                        texto.add(seleccion);
                    }
                }

                eventType = x.next();
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

That make a mistake because never go in if (x.getAttributeValue(null, "Book") != null) {

And when I used the debug mode tell me that x.depth() = 0

So, What am I doing wrong?

Upvotes: 1

Views: 105

Answers (1)

Jon Skeet
Jon Skeet

Reputation: 1502166

Book isn't an attribute - it's an element (a tag). You should use:

if (x.getName().equals("Book"))

to check whether you're on the Book element. However, that won't really help you much, as you're actually after the bookName, bookSection and memorization tags. I suspect you actually want (within the check for the START_TAG event):

if (x.getName().equals("bookName")) {
    seleccion.setBook(x.nextText());
} else if (x.getName().equals("bookSection")) {
    seleccion.setSection(x.nextText());
} else if (x.getName().equals("memorization")) {
    seleccion.setMemorization(x.nextText());
}

It's important to understand the difference between elements and attributes. For example, in:

<x y="z">Foo</x>

The element is <x>, y is an attribute with a value of "z", and Foo is the text within the x element.

Upvotes: 1

Related Questions