Android:在不同的Android版本上使用SAX解析器发出问题

时间:2013-02-25 12:38:55

标签: android compatibility sax

我在使用SAX解析器解析不同Android版本上的一个非常简单的XML文件时遇到了问题。

使用摩托罗拉XT316与Android 2.3.3,代码工作正常。我可以看到它进入所有标签。但是当使用PanDigital和Android 2.1时,代码不起作用。它正确打开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的结束”。

我不知道为什么会发生这种情况并且无法修复它。任何帮助将不胜感激。

提前致谢。

1 个答案:

答案 0 :(得分:2)

此链接帮助我解决了问题code.google.com/p/android/issues/detail?id=11223

显然,在Android 2.1和更新版本之间SAX解析器的功能有所不同。它将元素的标记放在不同的参数上。我不知道它是不是一个bug,但它确实看起来像一个。

使其工作的方法是更改​​函数startElement和endElement。在下面实现。

public static String getCorrectName(String qName, String name) {//used to fix bug from the sax on version 2.1
    String _retValue = null;
    if (Build.VERSION.SDK_INT<=7) _retValue = localName;
    else _retValue = qName;

    return _retValue;
}
 //opening element tag
 @Override
 public void startElement (String uri, String name, String qName, Attributes atts)
{
String corretcName=getCorrectName(qName, name);
if(corretcName.equals("ARQUIVO"))
{
    Log.d("DataHandler", "Start of ARQUIVO");                           
}   
}
//closing element tag
 @Override
 public void endElement (String uri, String name, String qName)
{       
String corretcName=getCorrectName(qName, name);
if(corretcName.equals("ARQUIVO"))
{           
    Log.d("DataHandler", "End of ARQUIVO");
}       
}