Converting php array with attributes to XML -
i'm working on xml library, can create / parse xml's arrays/jsons. managed write parser xml_parser (and google :p) cause simplexml wasn't enough i'm doing.
i've managed create array looks that:
array ( [flow] => array ( [math] => array ( [apply] => array ( [lt] => array ( ) [apply] => array ( [divide] => array ( ) [apply] => array ( [minus] => array ( ) ) ) [otherstuff] => 0 ) ) [true] => array ( ) [true_attr] => array ( [xsi:type] => somestuff [id] => 2 ) ) [flow_attr] => array ( [id] => 0 [xmlns:xsi] => http://www.w3.org/2001/xmlschema-instance ) )
as can see, should ( not best example :p ):
<flow id="0"> <math> <lalaa/> <appyl> </apply> </math> </flow>
note empty arrays should end /> example , , on
as can see separated node self node_attr contains attrs of nodes. flow_attr, true_attr.
anyone have idea how convert array xml? i'm lost , don't know do.
thanks help!
function recurse2xml ($array, &$string = "") { foreach ($array $key => $subarray) { if (substr($key, -5) == "_attr") continue; $attrs = ""; if (isset($array["$key_attr"])) foreach ($array["$key_attr"] $attr => $value) $attrs .= " $attr='".str_replace($value, "'", "\\'")."'"; if (empty($subarray)) { $string .= "<$key$attrs />" } else { $string .= "<$key$attrs>"; if (is_scalar($subarray)) $string .= $subarray; else recurse2xml($subarray, $string); $string .= "</$key>"; } } return $string; }
this function called recurse2xml($array);
expands array tree xml tree (string form).
Comments
Post a Comment