SAX 解析器从 endelement 获取属性
Posted
技术标签:
【中文标题】SAX 解析器从 endelement 获取属性【英文标题】:SAX parser get attribute from endelement 【发布时间】:2014-03-16 08:30:53 【问题描述】:我使用 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 登录
更新
我在 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 看看:
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
【问题讨论】:
你能发布你的代码来展示你到目前为止所做的尝试吗? 【参考方案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()
方法中检索任何属性的原因。因此,如果您想从 XML
中检索任何 attributes
,则必须在 startElement(String uri, String localName, String qName, Attributes attributes)
方法中检索它们。
【讨论】:
【参考方案2】:将 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;
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
输出将是:-
8
ComponentInfocom.designrifts.ultimatethemeui/ultimatethemeui.themeactivity icon
ComponentInfocom.chrislacy.actionlauncher.pro/com.chrislacy.launcher.Launcher apps_actionlauncherpro
ComponentInfocom.teslacoilsw.launcher/com.android.launcher2.Launcher apps_novalauncher
ComponentInfocom.teslacoilsw.launcher.prime/.NovaLauncherPrimeActivity apps_novalauncher
ComponentInfocom.anddoes.launcher/com.anddoes.launcher.Launcher apps_apexlauncher
ComponentInfocom.anddoes.launcher.pro/com.anddoes.launcher.pro.ApexLauncherProActivity apps_apexlauncher
ComponentInfoorg.adw.launcher/org.adw.launcherlib.Launcher apps_adwlauncher
ComponentInfoorg.adwfreak.launcher/org.adw.launcherlib.Launcher apps_adwex
【讨论】:
我做不到。我从其他应用程序中获取该文件。 我有这样的文件:github.com/designrifts/Ultimate_Theme_UI_Template/blob/master/… 但有 1000 行:P 像这样:public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException Log.d("INFO", startElement " + localName + ", " + qName + ", " + attributes);
那行不通。不要记录任何内容。以上是关于SAX 解析器从 endelement 获取属性的主要内容,如果未能解决你的问题,请参考以下文章
在 JAVA 中使用 SAX 解析器从 XML 文件中提取文本节点