我在使用 SAX 解析器解析不同 Android 版本上的非常简单的 XML 文件时遇到问题。
使用带有 Android 2.3.3 的 Motorola XT316 代码可以正常工作。我可以看到它进入了所有标签。但是当使用带有 Android 2.1 的 PanDigital 时,代码不起作用。它正确打开 XML 文件,但不输入任何标签。
解析器的代码:
public MyParser(Context context ) {
super();
theContext=context;
getData();
}
//start of the XML document
@Override
public void startDocument () {
Log.d("DataHandler", "Start of XML document");
}
//end of the XML document
@Override
public void endDocument () { Log.d("DataHandler", "End of XML document"); }
//opening element tag
@Override
public void startElement (String uri, String name, String qName, Attributes atts)
{
if(qName.equals("ARQUIVO"))
{
Log.d("DataHandler", "Start of ARQUIVO");
}
}
//closing element tag
@Override
public void endElement (String uri, String name, String qName)
{
if(qName.equals("ARQUIVO"))
{
Log.d("DataHandler", "End of ARQUIVO");
}
}
//element content
public void characters (char ch[], int start, int length)
{
}
public void getData()
{
//take care of SAX, input and parsing errors
try
{
//set the parsing driver
System.setProperty("org.xml.sax.driver","org.xmlpull.v1.sax2.Driver");
//create a parser
SAXParserFactory parseFactory = SAXParserFactory.newInstance();
parseFactory.setNamespaceAware(true);
SAXParser xmlParser = parseFactory.newSAXParser();
//get an XML reader
XMLReader xmlIn = xmlParser.getXMLReader();
//instruct the app to use this object as the handler
xmlIn.setContentHandler(this);
InputStreamReader xmlStream = new InputStreamReader(theContext.getResources().openRawResource(R.raw.arquivos));
//build a buffered reader
BufferedReader xmlBuff = new BufferedReader(xmlStream);
xmlIn.parse(new InputSource(xmlBuff));
}
catch(SAXException se) {
Log.e("AndroidTestsActivity","SAX Error " + se.getMessage());
}
catch(IOException ie) {
Log.e("AndroidTestsActivity","Input Error " + ie.getMessage());
}
catch(Exception oe) {
Log.e("AndroidTestsActivity","Unspecified Error " + oe.getMessage());
}
}
}
XML 文件:
<?xml version="1.0" encoding="UTF-8"?>
<ARQUIVOS>
<ARQUIVO
arquivo_caminho="http://192.168.0.60:8099/screen.xml"
arquivo_nome="screen.xml" />
<ARQUIVO
arquivo_caminho="http://192.168.0.60:8099/Images/T_0.png"
arquivo_nome="T_0.png" />
<ARQUIVO
arquivo_caminho="http://192.168.0.60:8099/Images/T_1.png"
arquivo_nome="T_1.png" />
<ARQUIVO
arquivo_caminho="http://192.168.0.60:8099/Images/T_2.png"
arquivo_nome="T_2.png" />
<ARQUIVO
arquivo_caminho="http://192.168.0.60:8099/Images/T_3.png"
arquivo_nome="T_3.png" />
<ARQUIVO
arquivo_caminho="http://192.168.0.60:8099/Images/T_4.png"
arquivo_nome="T_4.png" />
</ARQUIVOS>
我可以看到它显示了消息“XML 文档开始”和“XML 文档结束”,但它们之间没有显示消息“ARQUIVO 开始”和“ARQUIVO 结束”。
我不知道为什么会发生这种情况并且无法修复它。任何帮助将不胜感激。
提前致谢。