Home  >  Article  >  Backend Development  >  php解析xml提示Invalid byte 1 of 1-byte UTF-8 sequence错误的处理方法_php技巧

php解析xml提示Invalid byte 1 of 1-byte UTF-8 sequence错误的处理方法_php技巧

WBOY
WBOYOriginal
2016-05-17 08:52:57900browse

错误提示
Invalid byte 1 of 1-byte UTF-8 sequence
原因分析
在中文版的window下java的默认的编码为GBK,也就是所虽然我们标识了要将xml保存为utf-8格式但实际上文件是以GBK格式来保存的,所以这也就是为什么能够我们使用GBK、GB2312编码来生成xml文件能正确的被解析,而以UTF-8格式生成的文件不能被xml解析器所解析的原因。

把xml的encoding属性值UTF-8改为UTF8
org.xml.sax.SAXParseException: Content is not allowed in trailing section
把先要解析和字符串trim()一下即可解决问题。
解决:
1、最简单就是把改成
2、或者把xml打开另存的时候把字符集改为UTF-8后保存
或改程序

复制代码 代码如下:

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