Invalid byte 1 of 1-byte UTF-8 sequence Solution _php Tutorial

Source: Internet
Author: User
Tags xml parser
When parsing xml with PHP prompt invalid byte 1 of 1-byte UTF-8 sequence error, this problem I Baidu verified that is coding problem, the result I put the code to deal with really KO, below I to share the solution.

Error hints

Invalid byte 1 of 1-byte UTF-8 sequence

Cause analysis

In the Chinese version of the window under the Java default encoding is GBK, that is, although we have identified to save XML in the Utf-8 format but actually the file is saved in GBK format, so that is why we can use GBK, GB2312 encoding to generate XML files can be parsed correctly, while files generated in UTF-8 format cannot be parsed by the XML parser.


Change the encoding attribute value of XML UTF-8 to UTF8

Org.xml.sax.SAXParseException:Content isn't allowed in trailing section

Solve the problem by first parsing and string trimming ().

Solve:

1, the simplest is to change into

2, or to open the XML when the save character set to UTF-8 after the save

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 the Encoding method
XMLWriter writer = new XMLWriter (new FileWriter "D:\dom4j.xml"), of;


I'm using the Eclipse editor to do the following

Can be changed in Eclipse, in the Eclipse's function table [Project]→[properties],?? [Resources], in the right "text file encoding", the original Xing to Y?? ??, change? "utf-8".

http://www.bkjia.com/PHPjc/633192.html www.bkjia.com true http://www.bkjia.com/PHPjc/633192.html techarticle When using PHP parsing XML prompt Invalid byte 1 of 1-byte UTF-8 sequence error, this problem I Baidu verified that is coding problem, the result I put the code to deal with really ko, below I come ...

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