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
Change System.out.println to ur Log.ins ..
Item .java
package com.rofl;
public class Item {
private String component;
private String drawable;
public String getComponent() {
return component;
}
public void setComponent(String component) {
this.component = component;
}
public String getDrawable() {
return drawable;
}
public void setDrawable(String drawable) {
this.drawable = drawable;
}
}
SAXXMLHandler .java
package com.rofl;
import java.util.ArrayList;
import java.util.List;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
public class SAXXMLHandler extends DefaultHandler {
public static void main(String argv[]) {
try {
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser saxParser = factory.newSAXParser();
SAXXMLHandler handler = new SAXXMLHandler();
saxParser.parse("src/file.xml", handler);
System.out.println(SAXXMLHandler.itemList.size());
for(Item item:itemList)
{
System.out.println(item.getComponent()+"\t\t"+item.getDrawable());
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static List<Item> itemList = new ArrayList<Item>();
private Item item;
public SAXXMLHandler() {
itemList = new ArrayList<Item>();
}
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 Item();
if (qName.equalsIgnoreCase("item")) {
item.setComponent(attributes.getValue("component"));
item.setDrawable(attributes.getValue("drawable"));
itemList.add(item);
}
}
public void endElement(String uri, String localName, String qName)
throws SAXException {
}
}
output will be:-
8
ComponentInfo{com.designrifts.ultimatethemeui/ultimatethemeui.themeactivity} icon
ComponentInfo{com.chrislacy.actionlauncher.pro/com.chrislacy.launcher.Launcher} apps_actionlauncherpro
ComponentInfo{com.teslacoilsw.launcher/com.android.launcher2.Launcher} apps_novalauncher
ComponentInfo{com.teslacoilsw.launcher.prime/.NovaLauncherPrimeActivity} apps_novalauncher
ComponentInfo{com.anddoes.launcher/com.anddoes.launcher.Launcher} apps_apexlauncher
ComponentInfo{com.anddoes.launcher.pro/com.anddoes.launcher.pro.ApexLauncherProActivity} apps_apexlauncher
ComponentInfo{org.adw.launcher/org.adw.launcherlib.Launcher} apps_adwlauncher
ComponentInfo{org.adwfreak.launcher/org.adw.launcherlib.Launcher} apps_adwex
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.