我正在尝试将XML文件解析为内存,因此我可以将其扩展到listview。问题是我只能保留其最后一个注册表。
我想我错过了一些东西,但我不知道它是什么。
在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;
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);
}
}
答案 0 :(得分:1)
该问题与todo
标记有关。每次在startElement
override
中获取该标记时,您startElement
数据的当前值:将其从public static XMLGettersSetters data = new XMLGettersSetters();
中删除并在声明时将其实例化
{{1}}