Friday, November 15, 2019

java - SAX parser get attribute from endelement

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:







Code:



public class SAXXMLHandler extends DefaultHandler {



private ArrayList items;
private String tempVal;
private itemsList tempEmp;


private PackageManager manager;
private String packName;

public SAXXMLHandler(PackageManager manager, String packName) {
items = new ArrayList();

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 items;
private itemsList item;

public SAXXMLHandler() {
items = new ArrayList();
}

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

No comments:

Post a Comment

hard drive - Leaving bad sectors in unformatted partition?

Laptop was acting really weird, and copy and seek times were really slow, so I decided to scan the hard drive surface. I have a couple hundr...