douhanzhen8927 2011-02-25 22:01
浏览 70
已采纳

如何使用simpleXML解析PHP中不在utf-8中的xml文件?

I am using simpleXML in PHP to fetch xml file. The thing is that xml file's encoding is not in utf-8. It is in euc-kr.

<?xml version="1.0" encoding="euc-kr" ?> 

So, if I used the simpleXML to fetch the data, the text which is not in english, just crash. (but it correctly prints structures and everything...its just that text in Korean crash) Is there anyway that I can set encoding for simpleXML?

OR is there anyway that I can change the xml file to encoding with utf-8??

I just tried using str_replace('euc-kr','utf-8',$data) to change the words, but that does not seem to change the original file types. Because it causes an error like

Input is not proper UTF-8, indicate encoding ! Bytes: 0xB0 0xB3 0xC6 0xF7

How can I resolve this problem? By the way, I have no permission to change the contents of the xml file

  • 写回答

2条回答 默认 最新

  • duanbei2914 2011-02-25 22:04
    关注

    Assuming that you want to get the same charset out, I would use DomDocument. But if you want simplexml, you could load it through DomDocument then convert to simplexml:

    $dom = new DomDocument('1.0', 'euc-kr');
    $dom->load('file.xml'); // or loadXml($string);
    $simple = simplexml_import_dom($dom->documentElement);
    

    Then when you're done, just $dom->save($filename);...

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里