我使用SAX XML Parser时使用:
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException
我可以获得属性。 但我需要从 public void endElement
获取属性解析类似的东西:
<item name="test" value="somedata" />
代码:
公共类SAXXMLHandler扩展了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);
}
不要从startElement开始logcat
更新
我在片段中使用:
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看:
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 {
}
}
仍然没有:/
我解析的其他应用程序中的XML文件 http://pastebin.com/5GEthfmU
答案 0 :(得分:1)
将System.out.println更改为Log.ins ..
项目.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;
}
}
SAX XML Handler .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 {
}
}
输出将是: -
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
答案 1 :(得分:1)
在给定的XML行中..
<item name="test" value="somedata" />
name
和value
是只能在startElement()
方法中检索的属性。因为Attributes attributes
参数只传递给startElement(String uri, String localName, String qName, Attributes attributes)
方法。如果您查看endElement(String uri, String localName, String qName)
方法,则没有Attributes attributes
。这就是您无法从endElement()
方法检索任何属性的原因。因此,如果您想从attributes
检索任何XML
,则必须在startElement(String uri, String localName, String qName, Attributes attributes)
方法中检索它们。