使用 DOMDocument 创建映像标记失败


Creating an image Tag with DOMDocument fails

我正在使用DOMDocument来生成XML,并且此XML必须具有image-Tag。

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

$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>
如果我将"图像"

更改为"图像"甚至"img",它可以工作。当我从 PHP 5.3.10 切换到 5.3.8 时,它也可以工作。

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

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

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

这花了我两个小时。

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

第 44 行的$imageAction->getAction()是否有可能返回"img"?你var_dump()过吗?我不明白 DOM 在任何情况下如何将"图像"转换为"img"。

我认为它的行为就像一个"html文档"尝试添加版本号"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>