ID:               36795
 Comment by:       rob at electronicinsight dot com
 Reported By:      john at carney dot id dot au
 Status:           No Feedback
 Bug Type:         DOM XML related
 Operating System: Windows/Linux
 PHP Version:      5.1.2
 New Comment:

A little hack to get around this bug:

function &safe_add_child(&$sxml, $name, $value) {
  $safe_value = preg_replace('/&(?!\w+;)/', '&', $value);
  return $sxml->addChild($name, $safe_value);
}


Previous Comments:
------------------------------------------------------------------------

[2008-02-08 20:09:37] moshe at varien dot com

PHP 5.2.4
Looks like the problem appears when there's node already exists being
overwritten

// works ok, doesn't require encoding:
$a = simplexml_load_string('<a/>'); 
$a->b = "& < ' ";

// doesn't work, requires encoding:
$a = simplexml_load_string('<a><b>test</b></a>'); 
$a->b = "& < ' "; 

// doesn't work, always requires encoding
$a->addChild('b', "& < '");
$a->addAttribute('b', "& < '");

// works ok, never requires encoding
$a['b'] = "& < '";

------------------------------------------------------------------------

[2007-11-27 14:03:55] oscar at cdcovers dot to

I tried the workaround below and it seems to work:

$xml->addChild('element', '');
$xml->element = str_replace("&", "&amp;", "value of the element");

------------------------------------------------------------------------

[2007-09-28 06:40:20] ocus51 at gail dot com

Hi,

I'm still experiencing this problem :
 - PHP Version          5.2.0-8+etch7
 - DOM/XML API Version  20031129
 - libxml Version       2.6.27

------------------------------------------------------------------------

[2006-12-06 11:49:37] philippe dot levan_nospam at kitpages dot fr

Hi,

I have the same problem. My config is :
- PHP 5.2
- libxml Version 2.6.16
---------
<?php
$xmlStr = "<?xml version='1.0' encoding='UTF-8'?><root></root>";
$xml = new SimpleXMLElement($xmlStr);
$xml->addChild("foo",utf8_encode("start < > end"));
echo "foo tag added ok";
$xml->addChild("bar",utf8_encode("start & end"));
echo "error on bar tag because of &amp;";
$result = $xml->asXML();
echo "<pre>".htmlentities($result)."</pre>";
?>
-------------
you can run this script at :
http://www.kitpages.fr/test/bugSimpleXml.php

------------------------------------------------------------------------

[2006-06-19 14:30:10] php dot net at jax dot be

In PHP 4.2.2 I have the same issue.
<code>
<?php
        require_once("sms/messageRelay.class.php");
        $doc = domxml_new_doc("1.0");
        $msg = $doc->create_element("message");
        $msg->set_attribute("type", "MT");
        $doc->append_child($msg);
        $auth = $doc->create_element("authentication");
        $str = "abc < d & e > f";
        $auth->set_content($str);
        $msg->append_child($auth);
        
        echo "<pre>";
        echo $doc->dump_mem(true, "UTF-8");     
        echo "</pre>";
?>
</code>
It chokes on the &.

------------------------------------------------------------------------

The remainder of the comments for this report are too long. To view
the rest of the comments, please view the bug report online at
    http://bugs.php.net/36795

-- 
Edit this bug report at http://bugs.php.net/?id=36795&edit=1

Reply via email to