java - Alternative to built-in XmlPullParser with good encoding support -


i'm porting project written blackberry (java) android. project contains xml parsing classes written against org.xmlpull.v1.xmlpullparser interface. actual parser instance injected in classes outside.

this app parses xml files encoded in iso-8859-15 (aka latin 9). can't use utf-8, unfortunately need stick encoding.

the old blackberry project used kxml2 pull parser. in android trying use built-in parser can obtained this:

xmlpullparser parser = xml.newpullparser(); 

and configure char encoding:

parser.setinput(<input stream>, "iso-8859-15"); 

the problem parser not support char encoding. exception thrown:

org.xmlpull.v1.xmlpullparserexception: error parsing document. (position:line -1, column -1) caused by: org.apache.harmony.xml.expatparser$parseexception: @ line 1, column 0: unknown encoding. 

and it's odd because know android supports encoding. proof line runs no exceptions:

string test  = new string("hi".getbytes(), "iso-8859-15"); 

however, if configure parser different encoding, utf-8 or latin-1, works.

next thing tried use old project's parser (kxml2) in android, got new errors:

org.xmlpull.v1.xmlpullparserexception: unexpected type (position:end_document null@9:1 in java.io.inputstreamreader@43e97088) 

even if use without issues, kxml2 hasn't received support in last years (last version released in 2006), i'd use android's pull parser if possible, more robust , have better performance.

i can fool default parser calling parser.setinput(bais, "iso-8859-1");, because way ignores encoding in xml declaration in file, , works because both charsets have same number of characters , of them same. way looking @ source code think uses latin-1 when receives input in latin-9 , hence produces strings in latin-9.

is there reason default xml pull parser not supporting iso-8859-15? there alternative pull parsing library char encoding support?

thanks in advance.


update: when wrote question have tested default parser in os 2.2 , 2.3. however, reading javadoc xml.newpullparser found this:

note: slower sax parser, , it's not implemented. if need fast, implemented pull parser, use this. if need complete implementation, use kxml.

and in fact, when testing default parser in os 4.x, got second exception. looks os 4 built-in parser kxml!!

well, looks it's difficult find xmlpullparser library, i'm going use kxml's parser following advice in javadocs xml.newpullparser factory method. (i've not found note in online javadocs, in eclipse's javadoc window. maybe i'm using old javadocs , note later removed after android started using kxml built-in parser).

as exception thrown when using kxml's parser, this:

org.xmlpull.v1.xmlpullparserexception: unexpected type (position:end_document null@9:1 in java.io.inputstreamreader@43e97088) 

it turned out caused code. in initial port, realized android's built-in parser included in froyo , gingerbread did not advance next tag after calling parser.nexttext. added parser.nextag lines here , there make work. switched again kxml keep lines, made kxmlparser instance mess when processing end of file. exception thrown when calling nexttag after having reached end of file. explained in docs nexttag:

call next() , return event if start_tag or end_tag otherwise throw exception.


Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

c++ - qgraphicsview horizontal scrolling always has a vertical delta -