0

我正在尝试将 XML 文件解析到内存,所以我可以将它膨胀到一个列表视图。问题是我只能保留它的最后一个寄存器。

我想我错过了一些东西,但我不知道它是什么。

在 LogCat,我可以看到所有的寄存器都在流动……

谢谢您的帮助!

活动文件:

  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;

XML处理程序:

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

}

4

1 回答 1

1

问题与todo标签有关。每次您在您的内部获取该标签时startElementoverride数据的当前值:将其从中删除startElement并在声明时实例化它

public static XMLGettersSetters data = new XMLGettersSetters();
于 2013-06-15T18:55:04.427 回答