###################################################################################
# Xml_unserialize:takes Raw Xmlasa parameter (a string)
#andreturns an equivalent PHP data structure
###################################################################################
function& xml_unserialize (& $xml) {
$xml _parser= &newxml ();
$data = & $xml _parser->parse ($xml);
$xml _parser->destruct ();
###################################################################################
# xml_serialize:serializes Any PHP data structure into XML
# takes one parameter:the data to serialize. Must be Anarray.
###################################################################################
function& xml_serialize (& $data, $level = 0, $prior _key= NULL) {
if ($level = = 0) {Ob_start (); Echo ' <?xml version= ' 1.0 '?> ', ' \ n ';}
while (the list ($key, $value) = each ($data))
if (!strpos ($key, ' attr ')) #ifit ' s not a attribute
#we don ' t treat attributes by themselves, Soforan emptyempty element
# that has attributes your still need to set the element to NULL
if (Is_array ($value) andarray_key_exists (0, $value)) {
Xml_serialize ($value, $level, $key);
$tag = $prior _key $prior _key: $key;
Echostr_repeat ("T", $level), ' < ', $tag;
if (array_key_exists ("$key attr", $data)) {#ifthere ' s an attributeforthis element
while (the list ($attr _name, $attr _value) = each ($data ["$key attr"])
Echo ', $attr _name, ' = ', Htmlspecialchars ($attr _value), ' "';
Reset ($data ["$key attr"]);
if (Is_null ($value)) echo "/>\n";
ElseIf (!is_array ($value)) echo ' > ', Htmlspecialchars ($value), "</$tag >\n";
Elseecho ">\n", Xml_serialize ($value, $level + 1), str_repeat ("T", $level), "</$tag >\n";
if ($level = = 0) {$str = &ob_get_contents (); Ob_end_clean (); return$str;}
###################################################################################
# Xmlclass:utilityclassto be used with PHP ' s XML handling functions
###################################################################################
Var$parser; #a reference to the XML parser
Var$document; #the entire XML structure built up so far
Var$parent; #a pointer to the current parent-the parent would be Anarray
Var$stack; #a stack of the most recent parent at each nesting level
Var$last_opened_tag; #keeps track of the last tag opened.
$this->parser = &xml_parser_create ();
Xml_parser_set_option (& $this->parser, xml_option_case_folding, false);
Xml_set_object (& $this->parser, & $this);
Xml_set_element_handler (& $this->parser, ' open ', ' close ');
Xml_set_character_data_handler (& $this->parser, ' data ');
Functiondestruct () {xml_parser_free (& $this->parser);}
function& Parse (& $data) {
$this->document =array ();
$this->parent = & $this->document;
Returnxml_parse (& $this->parser, & $data, True)? $this->document:null;
Functionopen (& $parser, $tag, $attributes) {
$this->data = '; #stores Temporary CDATA
$this->last_opened_tag = $tag;
if (Is_array ($this->parent) andarray_key_exists ($tag, $this->parent)) {#ifyou ' ve seen this tag before
if (Is_array ($this->parent[$tag]) andarray_key_exists (0, $this->parent[$tag]) {#ifthe keys are numeric
#this is the Thirdorlater instance Of$tagwe ' ve come across
$key = Count_numeric_items ($this->parent[$tag]);
#this is the second instance of$tagthat we ' ve seen. Shift Around
if (array_key_exists ("$tag attr", $this->parent)) {
$arr =array (' 0 attr ' =>& $this->parent["$tag attr"], & $this->parent[$tag]);
unset ($this->parent["$tag attr"]);
$arr =array (& $this->parent[$tag]);
$this->parent[$tag] = & $arr;
$this->parent = & $this->parent[$tag];
if ($attributes) $this->parent["$key attr"] = $attributes;
$this->parent = & $this->parent[$key];
$this->stack[] = & $this->parent;
Functiondata (& $parser, $data) {
if ($this->last_opened_tag!= NULL) #you don ' t need to store whitespace in between tags
Functionclose (& $parser, $tag) {
if ($this->last_opened_tag = = $tag) {
$this->parent = $this->data;
$this->last_opened_tag = NULL;
Array_pop ($this->stack);
if ($this->stack) $this->parent = & $this->stack[count ($this->stack)-1];
Functioncount_numeric_items (& $array) {
Returnis_array ($array) Count (Array_filter (Array_keys ($array), ' Is_numeric '): 0;