Error message
Invalid byte 1 of 1-byte UTF-8 sequence
Cause analysis
In Windows of the Chinese version, java is encoded as GBK by default, that is, although we have identified that we want to save xml in UTF-8 format, the files are actually saved in GBK format, so that is why we can use GBK, GB2312 encoding to generate xml files can be correctly parsed, and files generated in UTF-8 format cannot be parsed by the xml parser.
Change the xml encoding attribute value UTF-8 to UTF8
Org. xml. sax. SAXParseException: Content is not allowed in trailing section
Parse and trim () the string to solve the problem.
Solution:
1. The simplest is to <? Xml version = "1.0" encoding = "UTF-8"?> Change to <? Xml version = "1.0" encoding = "gbk"?>
2, or to open another xml when the character set to UTF-8 save
Or change the program
The code is as follows: |
Copy code |
SAXReader reader = new SAXReader (); Org. dom4j. Document document = reader. read ("D: \ ha. xml "); OutputFormat of = new OutputFormat (); Of. setEncoding ("UTF-8"); // change the encoding method XMLWriter writer = new XMLWriter (new FileWriter "d: \ dom4j. xml"), ); |
I am using the eclipse editor as follows:
It can be changed in Eclipse. In the eclipse Menu [Project] & rarr; [Properties],? [Resources], right? Text file encoding, set the original? Why ?? ??, Change? 「 UTF-8 」.