_children); } function &children() { return $this->_children; } function &get_child ($name) { $seg = substr ($name, 0, strcspn ($name, "/")); if (is_array ($this->_children)) { foreach ($this->_children as $child) { if (strcasecmp ($child->get_name(), $seg) == 0) { if ($seg == $name) { return $child; } else { return $child->get_child (substr ($name, strcspn ($name, "/") + 1)); } } } } // printf ("get_child in %s for %s(%s) returning null\n", // $this->_name, $name, $seg); return NULL; } function &get_data ($name) { // printf ("Get data for %s\n", $name); $tmp = $this->get_child ($name); if ($tmp) return $tmp->_children[0]->get_data(); else return NULL; } function &xml_tree_node_c ($name, $attribs) { $this->_name = $name; $this->_attribs = $attribs; $this->_children = array(); return $this; } function set_attribs ($attribs) { $this->_attribs = $attribs; } function &add_child (&$xml_node) { return ($this->_children[] = $xml_node); } function &new_child ($name, $attribs) { $tmp = &$this->add_child (new xml_tree_node_c ($name, $attribs)); return $tmp; } function &new_child_data ($name, $attribs, $data) { $tmp = &$this->add_child (new xml_tree_node_c ($name, $attribs)); $tmp->add_child (new xml_text_node_c ($data)); return $tmp; } function get_name () { return $this->_name; } function print_node ($recurse, $rec_level) { $result = ""; for ($i = 0; $i < 2*$rec_level; $i++) $result .= " "; $result .= "<" . $this->_name; if (is_array ($this->_attribs)) { foreach ($this->_attribs as $key => $val) { $result .= " " . $key . "='" . $val . "'"; } } $result .= ">"; $newl = 0; if ($recurse) { foreach ($this->_children as $child) { if (!$newl && (get_class ($child) != "xml_text_node_c")) { $newl = 1; $result .= "\n"; } $result .= $child->print_node ($recurse, $rec_level+1); } } if ($newl) for ($i = 0; $i < 2*$rec_level; $i++) $result .= " "; $result .= "_name . ">\n"; return $result; } }; class xml_text_node_c extends xml_node_c { var $_data; function &xml_text_node_c ($data) { $this->_data = $data; return $this; } function get_data () { return $this->_data; } function print_node ($recurse, $rec_level) { $result = ""; $result .= $this->_data; return $result; } }; function text_to_xml ($xml_text) { $xml_parser = xml_parser_create (); xml_parser_set_option ($xml_parser, XML_OPTION_CASE_FOLDING, FALSE); if (!xml_parse_into_struct ($xml_parser, $xml_text, &$values, &$index)) { die(sprintf("XML error: %s at line %d", xml_error_string(xml_get_error_code($xml_parser)), xml_get_current_line_number($xml_parser))); } xml_parser_free($xml_parser); // Now, make the objects $stackpos = 0; $stack = array(); foreach ($values as $val) { switch ($val["type"]) { case "open": if (count($stack)) { $stack[$stackpos] = & $stack[$stackpos-1]->new_child ($val["tag"], @$val["attributes"]); } else { $stack[$stackpos] = &new xml_tree_node_c ($val["tag"], @$val["attributes"]); } $stackpos++; break; case "complete": if (count($stack)) $stack[$stackpos] = & $stack[$stackpos-1]->add_child (new xml_tree_node_c ($val["tag"], @$val["attributes"])); else { $stack[$stackpos] = & new_xml_tree_node_c ($val["tag"], @$val["attributes"]); } $stack[$stackpos]->add_child (new xml_text_node_c (@$val["value"])); break; case "close": if ($stackpos > 0) { $stackpos--; } break; default: // printf ("UNHANDLED: %s\n", $val["type"]); // print_r ($val); } } return $stack[0]; }