Home > Backend Development > PHP Tutorial > Invalid byte 1 of 1-byte UTF-8 sequence solution_PHP tutorial

Invalid byte 1 of 1-byte UTF-8 sequence solution_PHP tutorial

WBOY
Release: 2016-07-13 10:43:10
Original
2819 people have browsed it

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
 代码如下 复制代码

 SAXReader reader = new SAXReader(); 
  org.dom4j.Document document = reader.read("D:ha.xml"); 
  OutputFormat of = new OutputFormat(); 
  of.setEncoding("UTF-8"); //改变编码方式 
XMLWriter writer = new XMLWriter(new FileWriter "d:dom4j.xml"), of); 

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);

I am using the eclipse editor and just do the following can be changed in Eclipse, in the eclipse menu [Project] → [Properties], [Resources], on the right "Text file encoding", change the original to y?? , change it to "UTF-8". http://www.bkjia.com/PHPjc/633192.html
www.bkjia.com
true
http: //www.bkjia.com/PHPjc/633192.htmlTechArticleWhen using php to parse xml, it prompts Invalid byte 1 of 1-byte UTF-8 sequence. This problem is wrong. Baidu checked and said it was an encoding problem. As a result, I processed the encoding and it turned out to be KO. Here I go...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template