2 ###################################################################################
4 # XML Library, by Keith Devens, version 1.0
5 # http://keithdevens.com/software/phpxml
7 # This code is Open Source, released under terms similar to the Artistic License.
8 # Read the license at http://keithdevens.com/software/license
10 ###################################################################################
12 ###################################################################################
13 # XML_unserialize: takes raw XML as a parameter (a string)
14 # and returns an equivalent PHP data structure
15 ###################################################################################
16 function & XML_unserialize(&$xml){
17 $xml_parser = &new XML();
18 $data = &$xml_parser->parse($xml);
19 $xml_parser->destruct();
22 ###################################################################################
23 # XML_serialize: serializes any PHP data structure into XML
24 # Takes one parameter: the data to serialize. Must be an array.
25 ###################################################################################
26 function & XML_serialize(&$data, $level = 0, $prior_key = NULL){
27 #assumes a hash, keys are the tag names
29 while(list($key, $value) = each($data)){
30 if(!strpos($key, ' attr')){ #if it's not an attribute
31 #... we don't treat attributes by themselves.
32 #note that implies that for an empty element that has attributes, you still
33 #need to set the element to NULL
35 $attributes = array();
36 if(array_key_exists("$key attr", $data)){ #if there's an attribute for this element
37 while(list($attr_name, $attr_value) = each($data["$key attr"]))
38 $attributes[] = $attr_name.'="'.htmlspecialchars($attr_value).'"';
39 reset($data["$key attr"]);
42 if(is_array($value) and array_key_exists(0, $value)){
43 #numeric array (note that you can't have numeric keys at two levels in a row)
44 $xml_parts .= XML_serialize($value, $level, $key);
46 if($prior_key) $key = $prior_key;
47 #(i.e. if we're in a numeric array, replace the number with the actual tag)
49 $xml_parts .= str_repeat("\t", $level).'<'.$key;
50 if($attributes) $xml_parts .= ' '.join(' ',$attributes);
53 $xml_parts .= " />\r\n";
54 elseif(!is_array($value))
55 $xml_parts .= '>'.htmlspecialchars($value)."</$key>\r\n";
57 $xml_parts .= ">\r\n".XML_serialize($value, $level+1).str_repeat("\t", $level)."</$key>\r\n";
62 if($level == 0) return "<?xml version=\"1.0\" ?>\r\n".$xml_parts;
65 ###################################################################################
66 # XML class: utility class to be used with PHP's XML handling functions
67 ###################################################################################
69 var $parser; #a reference to the XML parser
70 var $document; #the entire XML structure built up so far
71 var $parent; #a pointer to the current parent - the parent will be an array
72 var $stack; #a stack of the most recent parent at each nesting level
73 var $last_opened_tag; #keeps track of the last tag opened.
76 $this->parser = &xml_parser_create();
77 xml_parser_set_option(&$this->parser, XML_OPTION_CASE_FOLDING, 0);
78 xml_set_object(&$this->parser, &$this);
79 xml_set_element_handler(&$this->parser, 'open','close');
80 xml_set_character_data_handler(&$this->parser, 'data');
83 xml_parser_free(&$this->parser);
85 function & parse(&$data){
86 $this->document = array();
87 $this->parent = &$this->document;
88 $this->stack = array();
89 return xml_parse(&$this->parser, &$data, true) ? $this->document : NULL;
91 function open(&$parser, $tag, $attributes){
92 $this->data = array(); #stores temporary cdata
93 $this->last_opened_tag = $tag;
94 if(is_array($this->parent) and array_key_exists($tag,$this->parent)){ #if you've seen this tag before
95 if(is_array($this->parent[$tag]) and array_key_exists(0,$this->parent[$tag])){ #if the keys are numeric
96 #this is the third or later instance of $tag we've come across
97 $key = count_numeric_items($this->parent[$tag]);
99 #this is the second instance of $tag that we've seen
101 if(array_key_exists("$tag attr",$this->parent)){
102 $arr = array('0 attr'=>&$this->parent["$tag attr"], &$this->parent[$tag]); unset($this->parent["$tag attr"]);
104 $arr = array(&$this->parent[$tag]);
106 $this->parent[$tag] = &$arr;
109 $this->parent = &$this->parent[$tag];
114 $this->parent["$key attr"] = $attributes;
116 $this->parent[$key] = NULL; #it turns out you can take a reference to NULL :)
117 $this->parent = &$this->parent[$key];
118 $this->stack[] = &$this->parent;
120 function data(&$parser, $data){
121 if($this->last_opened_tag != NULL) #you don't need to store whitespace in between tags
122 $this->data[] = $data;
124 function close(&$parser, $tag){
125 static $just_closed = false;
126 if($this->last_opened_tag == $tag){
128 $this->parent = join('',$this->data);
129 $this->last_opened_tag = NULL;
131 array_pop($this->stack);
133 $this->parent = &$this->stack[count($this->stack)-1];
136 function count_numeric_items(&$array){
137 return is_array($array) ? count(array_filter(array_keys($array), 'is_numeric')) : 0;