note 69729 added to function.simplexml-element-addchild

From: Date: Mon, 18 Sep 2006 12:57:43 +0000
Subject: note 69729 added to function.simplexml-element-addchild
Groups: php.notes 
Request: Send a blank email to php-notes+get-117284@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. 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); } } ---- Server IP: 83.137.200.2 Probable Submitter: 195.1.31.157 ---- Manual Page -- http://www.php.net/manual/en/function.simplexml-element-addchild.php Edit -- https://master.php.net/note/edit/69729 Del: integrated -- https://master.php.net/note/delete/69729/integrated Del: useless -- https://master.php.net/note/delete/69729/useless Del: bad code -- https://master.php.net/note/delete/69729/bad+code Del: spam -- https://master.php.net/note/delete/69729/spam Del: non-english -- https://master.php.net/note/delete/69729/non-english Del: in docs -- https://master.php.net/note/delete/69729/in+docs Del: other reasons-- https://master.php.net/note/delete/69729 Reject -- https://master.php.net/note/reject/69729 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#117284) next »