PHP parsing XML hint Invalid byte 1 of 1-byte UTF-8 sequence error handling method _php technique

Source: Internet
Author: User
Tags error handling xml parser

Error tips
Invalid byte 1 of 1-byte UTF-8 sequence
Cause analysis
In the Chinese version of Windows, the default encoding of Java is GBK, which means that although we identify the XML to be saved as Utf-8 format but actually the file is saved in GBK format, so that's why we can use GBK, GB2312 encoding to generate XML files can be parsed correctly, and files generated in UTF-8 format cannot be parsed by the XML parser.

Change the encoding attribute value of the XML UTF-8 to UTF8
Org.xml.sax.SAXParseException:Content is isn't allowed in trailing
You can solve the problem by first parsing and string trim ().
Solve:
1, the simplest is to <?xml version= "1.0" encoding= "UTF-8"?> to <?xml version= "1.0" encoding= "GBK"?>
2, or the XML open to save when the character set changed to UTF-8 after saving
or change the procedure

Copy Code code 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 mode
XMLWriter writer = new XMLWriter (new FileWriter "D:\dom4j.xml"), of);

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.