note 124615 added to class.simplexmlelement

From: Date: Wed, 15 Jan 2020 08:09:40 +0000
Subject: note 124615 added to class.simplexmlelement
Groups: php.notes 
Request: Send a blank email to php-notes+get-213882@lists.php.net to get a copy of this message
Class with a method to transform array or object to XML: class MakeXML { public static function array_obj_to_xml($data, \SimpleXMLElement &$xmlObj) { foreach((array) $data as $key => $value) { if(is_numeric($key)) { $key = "n" . $key; } if(is_array($value) or is_object($value)) { $subnode = $xmlObj->addChild($key); self::array_obj_to_xml($value, $subnode); } else { $xmlObj->addChild($key, htmlspecialchars($value)); } } } } Example: $object = new \stdClass(); $object->example = "Try this class"; $objxml = new \SimpleXMLElement('<API/>'); MakeXML::array_obj_to_xml($objxml, $objxml); echo $objxml->asXML(); ---- Server IP: 45.112.84.4 Probable Submitter: 38.142.137.34 (proxied: 192.140.101.47) ---- Manual Page -- http://php.net/manual/en/class.simplexmlelement.php Edit -- https://master.php.net/note/edit/124615 Del: integrated -- https://master.php.net/note/delete/124615/integrated Del: useless -- https://master.php.net/note/delete/124615/useless Del: bad code -- https://master.php.net/note/delete/124615/bad+code Del: spam -- https://master.php.net/note/delete/124615/spam Del: non-english -- https://master.php.net/note/delete/124615/non-english Del: in docs -- https://master.php.net/note/delete/124615/in+docs Del: other reasons-- https://master.php.net/note/delete/124615 Reject -- https://master.php.net/note/reject/124615 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#213882) next »