Pregunta

I am trying to parse XML file to memory, so I can inflate it to a listview. The thing is that I can only keep its last register.

I think I am missing something but I don't know what it is.

At LogCat I can see all of the registers flowing...

Thanks for the help!

Activity file:

  try {

        /**
         * Create a new instance of the SAX parser
         **/
        SAXParserFactory saxPF = SAXParserFactory.newInstance();
        SAXParser saxP = saxPF.newSAXParser();
        XMLReader xmlR = saxP.getXMLReader();

        /** 
         * Create the Handler to handle each of the XML tags. 
         **/
        XMLHandler myXMLHandler = new XMLHandler();
        xmlR.setContentHandler(myXMLHandler);
        xmlR.parse(new InputSource(getAssets().open("company_details.xml")));

    } catch (Exception e) {
        System.out.println(e);
    }

    data = XMLHandler.data;

XMLHandler:

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import android.app.Application;

public class XMLHandler extends DefaultHandler {

String elementValue = null;
Boolean elementOn = false;

public static XMLGettersSetters data = null;

public static XMLGettersSetters getXMLData() {
    return data;
}

public static void setXMLData(XMLGettersSetters data) {
    XMLHandler.data = data;
}

/** 
 * This will be called when the tags of the XML starts.
 **/
@Override
public void startElement(String uri, String localName, String qName,
        Attributes attributes) throws SAXException {

    elementOn = true;

    if (localName.equals("todo"))
    {
        data = new XMLGettersSetters();
    } else if (localName.equals("CD")) {
        /** 
         * We can get the values of attributes for eg. if the CD tag had an attribute( <CD attr= "band">Akon</CD> ) 
         * we can get the value "band". Below is an example of how to achieve this.
         * 
         * String attributeValue = attributes.getValue("attr");
         * data.setAttribute(attributeValue);
         * 
         * */
    }
}

/** 
 * This will be called when the tags of the XML end.
 **/
@Override
public void endElement(String uri, String localName, String qName)
        throws SAXException {

    elementOn = false;

    /** 
     * Sets the values after retrieving the values from the XML tags
     * */ 
    if (localName.equalsIgnoreCase("day"))
        data.setDia(elementValue);
    else if (localName.equalsIgnoreCase("week_day"))
        data.setDiaSemana(elementValue);
    else if (localName.equalsIgnoreCase("text"))
        data.setTexto(elementValue);
}

/** 
 * This is called to get the tags value
 **/
@Override
public void characters(char[] ch, int start, int length)
        throws SAXException {

    if (elementOn) {
        elementValue = new String(ch, start, length);
        elementOn = false;
    }

}

}

XMLGettersSetters:

import java.util.ArrayList;

import android.util.Log;

/**
 *  This class contains all getter and setter methods to set and retrieve data.
 *  
 **/
public class XMLGettersSetters {

private ArrayList<String> day = new ArrayList<String>();
private ArrayList<String> weekday = new ArrayList<String>();
private ArrayList<String> text = new ArrayList<String>();

public ArrayList<String> getDay() {
    return day;
}

public void setDay(String dia) {
    this.day.add(day);
    Log.i("This is the company:", day);
}

public ArrayList<String> getWeekDay() {
    return weekday;
}

public void setWeekDay(String weekday) {
    this.weekday.add(weekday);
    Log.i("This is the year:", weekday);
}

public ArrayList<String> getText() {
    return texto;
}

public void setText(String texto) {
    this.texto.add(texto);
    Log.i("This is the year:", text);
}

}

¿Fue útil?

Solución

the issue is related to the todo tag. Every time you get that tag inside your startElement you override the current value of data: remove it from the startElement and instantiate it at declaration time

public static XMLGettersSetters data = new XMLGettersSetters();
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top