I use SAX XML Parser and when I use:
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException
I can get attributes. But I need get attributes from public void endElement
To parse something like that:
<item name="test" value="somedata" />
Code:
public class SAXXMLHandler extends DefaultHandler {
private ArrayList<itemsList> items;
private String tempVal;
private itemsList tempEmp;
private PackageManager manager;
private String packName;
public SAXXMLHandler(PackageManager manager, String packName) {
items = new ArrayList<itemsList>();
this.manager = manager;
this.packName = packName;
}
public void characters(char[] ch, int start, int length)
throws SAXException {
tempVal = new String(ch, start, length);
}
// Event Handlers
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
Log.d("INFO", "startElement " + localName + ", " + qName + ", " + attributes);
// reset
tempVal = "";
if (qName.equalsIgnoreCase("item")) {
// create a new instance of employee
tempEmp = new itemsList();
tempEmp.setName(attributes.getValue("name"));
}
}
public void endElement(String uri, String localName, String qName)
throws SAXException {
Log.d("INFO", "endElement " + localName + ", " + qName);
}
And don't logcat from startElement
UPDATE
I use in Fragment:
SAXXMLHandler handler = new SAXXMLHandler();
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser saxParser = factory.newSAXParser();
saxParser.parse(asset, handler);
items = SAXXMLHandler.icons;
Util.l(String.valueOf(SAXXMLHandler.icons.size())); //log
for(itemList item:SAXXMLHandler.icons)
{
Util.l(item.getComponent()+"\t\t"+item.getComponent()); //log
}
SAXXMLHandler look:
public class SAXXMLHandler extends DefaultHandler {
public static ArrayList<itemsList> items;
private itemsList item;
public SAXXMLHandler() {
items = new ArrayList<itemsList>();
}
public void characters(char[] ch, int start, int length)
throws SAXException {
}
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
item = new itemsList();
Log.d("INFO", "startElement " + localName + ", " + qName);
if (qName.equalsIgnoreCase("item")) {
item.setComponent(attributes.getValue("component"));
items.add(item);
}
}
public void endElement(String uri, String localName, String qName)
throws SAXException {
}
}
And still nothing :/
XML file in other app which I parse http://pastebin.com/5GEthfmU
In the given XML line..
<item name="test" value="somedata" />
name
and value
are attributes which only can be retrieved in startElement()
method. Because, Attributes attributes
parameter is only passed into startElement(String uri, String localName, String qName, Attributes attributes)
method. If you look at endElement(String uri, String localName, String qName)
method there has no Attributes attributes
. That's why you can't retrieve any attribute from endElement()
method. So, if you want to retrieve any attributes
from a XML
then you have to retrieve them inside startElement(String uri, String localName, String qName, Attributes attributes)
method.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With