shredding
shredding

Reputation: 5591

Creating an image Tag with DOMDocument fails

I'm using DOMDocument to generate a XML and this XML has to have an image-Tag.

Somehow, when I do (simplified)

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

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

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

it results in

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

If I change 'image' to 'images' or even 'img' it works. It does work as well when I switch from PHP 5.3.10 to 5.3.8.

Is this a bug or a feature? My guess is that DOMDocuments assumes that I want to build an HTML img Element ... Can I prevent this somehow?

Weird thing on top: I'm not able to reproduce the error in another script on the same server. But I do not catch the pattern ...

Here's a complete pastebin of the class, that's causing the error: http://pastebin.com/KqidsssM

Upvotes: 1

Views: 1079

Answers (3)

shredding
shredding

Reputation: 5591

That costed me two hours.

DOMDocument renders the XML correctly. The XML is returned by an ajax call and somehow the browser/javascript changes it to img before displaying it ...

Upvotes: 2

I think it is behaving as an "html doc" try to add a version number, "1.0"

code

<?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();

output:

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

and also you can use SimpleXML classes

Example :

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

    echo $response->asXML();

output :

 <?xml version="1.0" ?> 
    <actions>
      <image>
        <nodexy /> 
      </image>
   </actions>

Upvotes: 0

rodneyrehm
rodneyrehm

Reputation: 13557

Is it possible that $imageAction->getAction() of line 44 returns 'img'? Have you var_dump()ed that? I don't see how DOM would convert "image" to "img" under any circumstances.

Upvotes: 0

Related Questions