Php parsing xml prompt Invalid byte 1 of 1-byte UTF-8 sequence error handling method

Source: Internet
Author: User
Tags xml parser

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
Copy codeThe 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"); // change the encoding method
XMLWriter writer = new XMLWriter (new FileWriter "d: \ dom4j. xml"), );
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.