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