When using PHP to parse xml, it prompts that Invalid byte 1 of 1-byte UTF-8 sequence is wrong. Baidu checked this problem and said it was an encoding problem. As a result, I processed the encoding and it turned out to be KO. I will share the solution below. way.
Error message
Invalid byte 1 of 1-byte UTF-8 sequence
Cause Analysis
The default encoding of java under the Chinese version of window is GBK, that is, although we have marked that the xml should be saved in utf-8 format, the file is actually saved in GBK format, so this is why The reason why we can use GBK and GB2312 encoding to generate xml files can be parsed correctly, but the files generated in UTF-8 format cannot be parsed by the xml parser.
Change the encoding attribute value of xml from UTF-8 to UTF8
org.xml.sax.SAXParseException: Content is not allowed in trailing section
The problem can be solved by parsing and trim() the string first.
Solution:
1. The easiest way is to change to
2. Or when opening the xml and saving it as a file, change the character set to UTF-8 and save it
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 encoding method XMLWriter writer = new XMLWriter(new FileWriter "d:dom4j.xml"), of); |