Sax - ExpatParser$ParseException

前端 未结 1 1570
春和景丽
春和景丽 2021-01-13 08:22

I\'m making an Android application that reads an XML Internet. This application uses SAX to parse XML. This is my code for the part of parsing:

public Lector         


        
相关标签:
1条回答
  • 2021-01-13 08:50

    This could be a character encoding problem.
    As you can see, the invalid token error points to the line #4.
    In this line, you can find an acute (Meteorología) and a tilde (España). The XML header shows a ISO-8859-15 encoding value. As it's less common than UTFs or ISO-8859-1 encodings, this could result in a error when the SAXParser connects and try to convert the byte content into chars using your system default charset.

    Then, you'll need to tell the SAXParser which charset to use. A way to do so, is to pass an InputSource, instead of the URL, to the parse method. As an example:

    SAXParserFactory factory = SAXParserFactory.newInstance();
    SAXParser parser = factory.newSAXParser();
    
    InputSource is = new InputSource(url);
    is.setEncoding("ISO-8859-15");
    
    DefaultHandler lxmlr=new LibraryXMLReader() ;
    sp.parse(is, lxmlr);
    

    EDIT: It seems that Android VM does not support this encoding, throwing a org.apache.harmony.xml.ExpatParser$ParseException: At line 1, column 0: unknown encoding exception.
    As ISO-8859-15 it's mainly compatible with ISO-8859-1, except some specific characters (as you can see here), a workaround is changing the ISO-8859-15 value to ISO-8859-1 at the setEncoding method, forcing the parser to use a different but compatible charset encoding:

    is.setEncoding("ISO-8859-1");
    

    As it seems, as Android doesn't support the declared charset, it uses its default (UTF-8) and hence the parser can't use the XML declaration to choose the apropiate encoding.

    0 讨论(0)
提交回复
热议问题