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

Jul 13, 2016 am 10:43 AM
byte invalid php utf-8 xml use Method exist hour solve parse

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

 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...
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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

CakePHP Date and Time

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

CakePHP File upload

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

CakePHP Routing

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

CakePHP Project Configuration

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

Discuss CakePHP

CakePHP Quick Guide CakePHP Quick Guide Sep 10, 2024 pm 05:27 PM

CakePHP Quick Guide

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

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

See all articles