0

这是我的 XML 代码,我想从中读取我的 android 应用程序的图标标签。我正在使用 DefaultHandler 来读取 XML。我尝试使用字符功能,但它不起作用。所以请你帮忙。

<Screens>
<Elements>
<Screen>ABC</Screen>
<Item id="100">
<name>addr</name>
<type>abc</type>
<icon>xyz.png</icon>
<title>Map</title>
</Item>
</Elements>
</Screens>
4

2 回答 2

0
@Override
public void startElement(String uri, String localName, String qName,
    Attributes attributes) throws SAXException {
// TODO Auto-generated method stub
sb=new StringBuilder();
if(localName.equals("icon"))
{
    iconflag=true;
}
}

@Override
public void characters (char ch[], int start, int length) {
if (sb!=null && iconflag == true) {
    for (int i=start; i<start+length; i++) {
        sb.append(ch[i]);
    }
}
}

@Override
public void endElement(String uri, String localName, String qName)
    throws SAXException {
// TODO Auto-generated method stub
if(iconflag)
{
    info.setIcon(sb.toString().trim());
    iconflag=false;
}
}

这就是解决方案。

于 2012-05-29T12:37:57.843 回答
0

这可以通过使用 xstream 来实现。它的实现非常简单。您只需要为上述 xml 创建一个类层次结构,您就可以轻松地访问此 xml 文件中的任何元素。请阅读以下 2 分钟教程:http: //x-stream.github.io/tutorial.html

于 2012-05-29T12:26:56.557 回答