dtvp3625 2014-09-24 18:37
浏览 89

在PHP中将XML从UTF-8解码为ISO-8859-1

I'm trying to "decode" an XML file (and transforming it with XSLT), but I'm having trouble decoding both files. The scenario is as follows:

I have a site for data entry which is all encoded in ISO-8859-1 (our Oracle database is in that format, so I can't change it). The problem is, I have those 2 files (an XML to show the data entry form and and XSLT to transform it into HTML). Both files are saved in ISO-8859-1 encoding, and both have the corresponding header, i. e., , and whenever I read the files and show them in the browser, the special characters (ñ, á, ¿) are shown either as UTF-8 or as a question mark (depending on the method I use for showing), but never as the "normal" representation.

My code for showing the XML file is:

<?php
$xslString = file_get_contents("catalog.xsl");
$xslString = utf8_decode($xslString);
$xslDoc = simplexml_load_string($xslString);

$xmlString = file_get_contents("questionnaire.xml");
$xmlString = utf8_decode($xmlString);
$xmlDoc = simplexml_load_string($xmlString);

$proc = new XSLTProcessor();
$proc->importStylesheet($xslDoc);
?>

I already tried several combinations of DOMDocument, iconv, mb_convert_encoding, but they show the XML file as unencoded UTF, a question mark or a double question mark.

On the other hand, this also messes up my data entry, since if I want to enter one of those characters, they either show as ? or ?? on the corresponding data field on the DB, or they get truncated at the first special char (if I use iconv).

What am I missing? Is there a workaround? I can't convert anything to UTF-8 because of the database.

I hope I'm being clear enough, please excuse my English.

Thanks in advance!

  • 写回答

1条回答 默认 最新

  • douyiyang6317 2014-09-25 16:48
    关注

    Hope this helps others. In the end, there were two things:

    1) I was reading the XML/XSL files like this (in my original script):

    <?php
    $xmlDoc = new DOMDocument();
    $xmlDoc->loadXML($xmlFile);
    $xmlDoc->load("xmlfile.xml");
    ?>
    

    which effectively changed the encoding to UTF-8. I changed the lines to:

    <?php
    $xmlString = file_get_contents("xmlfile.xml");
    $xmlDoc = simplexml_load_string($xmlString);
    ?>
    

    removing the utf_decode statement, and it worked like a charm. Now I get my special chars on screen as they're intended. As a side effect, the data entered in the form is now saved correctly to my database, so I got two birds in one shot.

    评论

报告相同问题?

悬赏问题

  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥30 python代码,帮调试,帮帮忙吧