Invalid byte 1 of 1-byte UTF-8 sequence solution_PHP tutorial
Jul 13, 2016 am 10:43 AMWhen 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 <?xml version="1.0" encoding="UTF-8"?> to <?xml version="1.0" encoding="gbk"?>
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); |

Hot Article

Hot tools Tags

Hot Article

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian

How To Set Up Visual Studio Code (VS Code) for PHP Development
