dregduc63699 2012-11-13 07:41
浏览 44

从PHP生成的XML编码

I am doing an assignment for class where I have to use a Java Servlet running on Tomcat and have it message a php file to scrape IMDB for movie information and return it as XML to the servlet. It seems to not want to accept any encoding I give it as I continuously get XML tags such as the ones below.

      <result cover="url" title="Pok&#xE9;mon" year="1998 TV Series" director="N/A" rating="7.8" details="http://www.imdb.com/title/tt0176385/"/>

Where title of Pokemon should have an accent over the e («é»). I have the following php code to generate the xml. (Important parts only)

    <?php header("Content-Type: text/xml; charset=utf-8");
    $xml = new DOMDocument();
    $rsp = $xml->appendChild($xml->createElement("rsp"));
    $xml->encoding = 'utf-8';

    $titleNames[$i] = utf8_encode($title_tmp[1]);

    $results = $rsp->appendChild($xml->createElement("results"));
    $results->setAttribute("total", $tableRows);

    $item->setAttribute("title", $titleNames[$i]);

    echo $xml->saveXML();
    ?>

Any help would be greatly appreciated in figuring out how to correctly display special characters!

  • 写回答

1条回答 默认 最新

  • doujie3888 2012-11-13 09:15
    关注

    It's impossible to say what's wrong from your code fragments (which don't even run) but $xml->encoding = 'utf-8' should work. Please compare:

    $xml = new DOMDocument();
    $rsp = $xml->appendChild($xml->createElement("rsp"));
    $rsp->setAttribute("title", 'Pokémon');
    echo $xml->saveXML();
    /*
    <?xml version="1.0"?>
    <rsp title="Pok&#xE9;mon"/>
    */
    

    ... with:

    $xml = new DOMDocument();
    $xml->encoding = 'utf-8';
    $rsp = $xml->appendChild($xml->createElement("rsp"));
    $rsp->setAttribute("title", 'Pokémon');
    echo $xml->saveXML();
    /*
    <?xml version="1.0" encoding="utf-8"?>
    <rsp title="Pokémon"/>
    */
    

    (These snippets are expected to be saved as UTF-8).

    评论

报告相同问题?

悬赏问题

  • ¥15 怎么把多于硬盘空间放到根目录下
  • ¥15 Matlab问题解答有两个问题
  • ¥50 Oracle Kubernetes服务器集群主节点无法访问,工作节点可以访问
  • ¥15 LCD12864中文显示
  • ¥15 在使用CH341SER.EXE时不小心把所有驱动文件删除了怎么解决
  • ¥15 gsoap生成onvif框架
  • ¥15 有关sql server business intellige安装,包括SSDT、SSMS。
  • ¥15 stm32的can接口不能收发数据
  • ¥15 目标检测算法移植到arm开发板
  • ¥15 利用JD51设计温度报警系统