note 69729 modified in function.simplexml-element-addChild by danbrown
| From: | danbrown@php.net | Date: | Wed, 16 Sep 2009 16:39:22 +0000 |
| Subject: | note 69729 modified in function.simplexml-element-addChild by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-160746@lists.php.net to get a copy of this message | ||
Because SimpleXML transforms the values & to & whe reading from file or string, the values
becomes invalid for use when adding childs later on. This simple conversion to the example above
fixes this problem.
<?php
class XMLElement extends SimpleXMLElement
{
public function addElement (SimpleXMLElement $xmlTree,$root=false)
{
if($root)
{
$child = $this->addChild ($xmlTree->getName());
foreach ($xmlTree->attributes() as $k => $v)
{
$child->addAttribute($k,$v);
}
$child->addElement($xmlTree);
}
else
{
foreach ($xmlTree as $childName => $childTree)
{
$child = $this->addChild($childName,$this->fix_content((string) $childTree));
// this is not comletely correct
foreach ($childTree->attributes() as $k => $v)
{
$child->addAttribute($k,$v);
}
$child->addElement($childTree->children());
}
}
}
#When the xml is read by simplexml_load_file or simplexml_load_file the values that contains
& is converted to &. And then it is not possible to add these values without getting errors.
#THis function should fix this
public function fix_content($value) {
return str_replace('&', '&', $value);
}
}
?>
--was--
Because SimpleXML transforms the values & to & whe reading from file or string, the values
becomes invalid for use when adding childs later on. This simple conversion to the example above
fixes this problem.
<?php
class XMLElement extends SimpleXMLElement
{
public function addElement (SimpleXMLElement $xmlTree,$root=false)
{
if($root)
{
$child = $this->addChild ($xmlTree->getName());
foreach ($xmlTree->attributes() as $k => $v)
{
$child->addAttribute($k,$v);
}
$child->addElement($xmlTree);
}
else
{
foreach ($xmlTree as $childName => $childTree)
{
$child = $this->addChild($childName,$this->fix_content((string) $childTree));
// this is not comletely correct
foreach ($childTree->attributes() as $k => $v)
{
$child->addAttribute($k,$v);
}
$child->addElement($childTree->children());
}
}
}
#When the xml is read by simplexml_load_file or simplexml_load_file the values that contains
& is converted to &. And then it is not possible to add these values without getting errors.
#THis function should fix this
public function fix_content($value) {
return str_replace('&', '&', $value);
}
}
?>
http://php.net/manual/en/function.simplexml-element-addChild.php