android - java.lang.IllegalArgumentException: Illegal character (d83d) -
while saving xml data file in android app, exception when user puts emoji character. d83d character one, understand: http://apps.timwhitlock.info/unicode/inspect?s=%f0%9f%98%84 - smily 😄 character.
the relevant stack-trace:
java.lang.illegalargumentexception: illegal character (d83d) @ org.kxml2.io.kxmlserializer.reportinvalidcharacter(kxmlserializer.java:144) @ org.kxml2.io.kxmlserializer.writeescaped(kxmlserializer.java:130) @ org.kxml2.io.kxmlserializer.attribute(kxmlserializer.java:465)
the overarching question is: how fix it, users don't have app crashing on emojis...
the follow-up questions are:
is kxmlserializer supposed support emoji/unicode?
is there updated version somewhere? couldn't find far. lib actively maintained?
who maintaining kxmlserializer? http://kxml.org/ ? couldn't find there...
wouldn't updated version, if put on class-path, collide android built-in one?
what other xml-writing lib should/could use replace kxmlserializer?
edit: shall add, text app saves via xml, comes standard android edittext ui widget user enters text (i not unicode-level manipulation on string).
i same exception @ test device(api17).here's code:
final xmlserializer serializer = new xml.newserializer(); final stringwriter stringwriter = new stringwriter(); serializer.setoutput(stringwriter); serializer.startdocument("utf-8", true); serializer.starttag("", "xxxx"); .... serializer.endtag("", "xxxx"); serializer.enddocument(); writer.write(stringwriter.tostring()); writer.close();
but code ok @ api21 , api23.
in api17,the key code of kxmlserializer:
// begin android-changed: refuse output invalid characters // see http://www.w3.org/tr/rec-xml/#charsets definition. // no other java xml writer know of this, no java // xml reader know of able parse bad output we'd // otherwise generate. // note: tab, newline, , carriage return have been // handled above. boolean valid = (c >= 0x20 && c <= 0xd7ff) || (c >= 0xe000 && c <= 0xfffd); if (!valid) { reportinvalidcharacter(c); } if (unicode || c < 127) { writer.write(c); } else { writer.write("&#" + ((int) c) + ";"); } // end android-changed
in api23,the key code of kxmlserializer:
// begin android-changed: refuse output invalid characters // see http://www.w3.org/tr/rec-xml/#charsets definition. // no other java xml writer know of this, no java // xml reader know of able parse bad output we'd // otherwise generate. // note: tab, newline, , carriage return have been // handled above. boolean allowedinxml = (c >= 0x20 && c <= 0xd7ff) || (c >= 0xe000 && c <= 0xfffd); if (allowedinxml) { if (unicode || c < 127) { writer.write(c); } else { writer.write("&#" + ((int) c) + ";"); } } else if (character.ishighsurrogate(c) && < s.length() - 1) { writesurrogate(c, s.charat(i + 1)); ++i; } else { reportinvalidcharacter(c); } // end android-changed
the reason of exception,you can read answer of stephen c.
at last,i download latest code of class kxmlserializer kxml(https://sourceforge.net/projects/kxml/files/kxml2/2.3.0/).and add project,rename class testkxmlserializer.so use class as:
final xmlserializer serializer = new testkxmlserializer();
it's right api17,api20,api23.
Comments
Post a Comment