萨克斯 - ExpatParser $ ParseException的 [英] Sax - ExpatParser$ParseException

查看:198
本文介绍了萨克斯 - ExpatParser $ ParseException的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在做一个Android应用程序读取XML互联网。此应用程序使用SAX解析XML。这是我的code解析的部分:

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 LectorSAX(String url){
    try{
        SAXParserFactory spf=SAXParserFactory.newInstance();
        SAXParser sp = spf.newSAXParser();
        DefaultHandler lxmlr=new LibraryXMLReader() ;
        sp.parse(url, lxmlr);

        nodo=((LibraryXMLReader)lxmlr).getNodoActual();

    }catch(ParserConfigurationException e){ 
        System.err.println("Error de parseo en LectorSAX.java: "+e);
    }catch(SAXException e){
        System.err.println("Error de sax LectorSAX.java: " + e);
    } catch (IOException e){
        System.err.println("Error de  io LectorSAX.java: " + e);
    }
}

问题是,的SAXException发生。异常消息如下:

The problem is that SAXException occurs. The exception message is as follows:

org.apache.harmony.xml.ExpatParser $ ParseException的:在4号线,列   42:没有良好的(标记无效)

org.apache.harmony.xml.ExpatParser$ParseException: At line 4, column 42: not well-formed (invalid token)

不过,如果我把同样的code在一个普通的Java SE应用程序,此异常不会发生,一切工作正常。

However, if I put the same code in a normal Java SE application, this exception does not occur and everything works fine.

为什么同样code正常工作,在Java SE应用程序,而不是一个Android?而另一方面,如何解决这个问题呢?

Why the same code works fine in a Java SE application, not an Android?. On the other hand, How to solve the problem?.

感谢您的帮助。

问候。

推荐答案

这可能是一个字符编码的问题。
正如你所看到的,无效的令牌错误点到线#4。
在这条线,你可以找到一种急性(Meteorología的)和波浪号(西班牙的)。 XML标头显示的 ISO-8859-15 编码值。由于它比因此UTF或ISO-8859-1编码不常见的,这可能会导致一个错误,当SAXParser的连接,并尝试使用系统默认字符集的字节的内容转换成字符。

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.

然后,你需要告诉SAXParser的哪个字符集使用。一种方法这样做,是为了传递一个的InputSource ,代替的URL,给解析方法。作为一个例子:

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);

编辑: 看来,Android的虚拟机不支持这种编码,抛出一个 org.apache.harmony.xml.ExpatParser $ ParseException的:在第1行,列0:不详编码的异常< BR> 由于ISO-8859-15它主要是与ISO-8859-1兼容,除了一些特殊字符(如你所见这里),解决方法是改变 ISO-8859-15 ISO-8859-1 的该setEncoding方法,迫使解析器使用不同但兼容的字符编码​​:

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");

,因为它似乎,因为Android不支持声明的字符集,它使用默认值(UTF-8),因此解析器不能使用XML声明来选择apropiate编码。

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.

这篇关于萨克斯 - ExpatParser $ ParseException的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆