function xml_adopt($root, $new) { $node = $root->addChild($new->getName(), (string) $new); foreach ($new->attributes() as $attr => $value) { $node->addAttribute($attr, $value); } foreach ($new->children() as $ch) { xml_adopt($node, $ch); } }
function xml_adopt($root, $new, $namespace = null) { // first add the new node $node = $root->addChild($new->getName(), (string) $new, $namespace); // add any attributes for the new node foreach ($new->attributes() as $attr => $value) { $node->addAttribute($attr, $value); } // get all namespaces, include a blank one $namespaces = array_merge(array(null), $new->getNameSpaces(true)); // add any child nodes, including optional namespace foreach ($namespaces as $space) { foreach ($new->children($space) as $child) { xml_adopt($node, $child, $space); } } }
function add_map($student_id, $json_string) { $json_arr = json_decode($json_string, true); $xml_object = json_arr_to_xml($json_arr); $student_record = get_user_maps($student_id); if (empty($student_record)) { $xml_of_maps = simplexml_load_string("<maps></maps>"); xml_adopt($xml_of_maps, $xml_object); $map_as_string = xml_to_string($xml_of_maps); } else { $maps = get_user_maps($student_id); xml_adopt($maps, $xml_object); $map_as_string = xml_to_string($maps); } $query = "UPDATE Students SET MAPS='{$map_as_string}' WHERE STUDENT_ID={$student_id}"; exec_query($query); }