1

我正在使用 DOMDocument 生成一个 XML,这个 XML 必须有一个图像标签。

不知何故,当我这样做时(简化)

$response = new DOMDocument();
$actions = $response->createElement('actions');
$response->appendChild($actions);

$imageElement = $response->createElement('image'); 
$actions->appendChild($imageElement);

$anotherNode = $response->createElement('nodexy');
$imageElement->appendChild($anotherNode);

结果是

 <actions>
    <img>
    <node></node>
 </actions>

如果我将“image”更改为“images”甚至“img”,它会起作用。当我从 PHP 5.3.10 切换到 5.3.8 时,它也能正常工作。

这是错误还是功能?我的猜测是 DOMDocuments 假设我想构建一个 HTML img 元素......我能以某种方式阻止这种情况吗?

最奇怪的是:我无法在同一服务器上的另一个脚本中重现该错误。但我没有抓住模式......

这是该类的完整pastebin,导致错误: http: //pastebin.com/KqidsssM

4

3 回答 3

2

这花了我两个小时。

DOMDocument 正确呈现 XML。XML由ajax调用返回,浏览器/javascript在显示之前将其更改为img ...

于 2012-06-29T11:11:16.037 回答
0

第 44 行是否有可能$imageAction->getAction()返回“img”?你var_dump()编过吗?我看不出 DOM 在任何情况下如何将“image”转换为“img”。

于 2012-06-29T11:05:14.130 回答
0

我认为它表现为“html doc”尝试添加版本号“1.0”

代码

<?php

    $response = new  DOMDocument('1.0','UTF-8');
    $actions = $response->createElement('actions');
    $response->appendChild($actions);

    $imageElement = $response->createElement('image'); 
    $actions->appendChild($imageElement);

    $anotherNode = $response->createElement('nodexy');
    $imageElement->appendChild($anotherNode);

    echo $response->saveXML();

输出:

  <?xml version="1.0" encoding="UTF-8" ?> 
     <actions>
       <image>
         <nodexy /> 
       </image>
     </actions>

你也可以使用SimpleXML

例子 :

<?php
    $response = new SimpleXMLElement("<actions></actions>");
    $imageElement = $response->addChild('image');
    $imageElement->addChild("nodexy");

    echo $response->asXML();

输出 :

 <?xml version="1.0" ?> 
    <actions>
      <image>
        <nodexy /> 
      </image>
   </actions>
于 2012-06-29T11:22:49.617 回答