Solutions for XML files containing Unicode characters

Source: Internet
Author: User


In recent work, we need to use XML files for data storage. Because the content of this XML file is data from different data sources, we encountered several similar exceptions when parsing the XML file: invalid XML character (UNICODE: 0x9e ). (UNICODE: 0x8b ).

Exception in thread "Main" org. xml. Sax. saxparseexception; linenumber: 24; columnnumber: 180; invalid xml characters (UNICODE: 0x9e) are found in the element content of the document ).

At com.sun.org. Apache. xerces. Internal. util. errorhandlerwrapper. createsaxparseexception (unknown source)

At com.sun.org. Apache. xerces. Internal. util. errorhandlerwrapper. fatalError (unknown source)

At com.sun.org. Apache. xerces. Internal. impl. xmlerrorreporter. reporterror (unknown source)

At com.sun.org. Apache. xerces. Internal. impl. xmlerrorreporter. reporterror (unknown source)

At com.sun.org. Apache. xerces. Internal. impl. xmllistener. reportfatalerror (unknown source)

...

At the beginning, trying to modify the XML file in different formats, UTF-8, anbi and so on, are invalid (later understand, the character value has not changed), then go to find a solution, I tried to convert Unicode to UTF-8, but also looked for the relevant algorithms, but XML contains only a few Unicode characters, can not solve.

Later I found some documents and found that these characters are common: these invalid characters are used as the control code of the document processor in some documents (Microsoft selects the characters between 0x82 and 0x95 as the "smart" punctuation ), these are also reserved by Unicode as control encoding and are invalid in XML.

All Unicode characters in the following web page, corresponding UTF-8 characters, and its meaning

Http://www.utf8-chartable.de/unicode-utf8-table.pl? Utf8 = 0x


Finally, I converted these invalid Unicode characters into null characters (scala code)

    def UnicodeStringHandler(value:String) = {    val chs = value.toArray        for(i <- 0 until value.length()) {          if (chs(i) > 0xFFFD)            {            chs(i) = ‘ ‘;            }             else if (chs(i) < 0x20 && chs(i) != ‘\t‘ & chs(i) != ‘\n‘ & chs(i) != ‘\r‘)            {             chs(i) = ‘ ‘;            }            else if(chs(i) >= 0x80 && chs(i) <= 0x9f){               chs(i) = ‘ ‘;            }        }        new String(chs);    }

Solutions for XML files containing Unicode characters

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.