]> git.sur5r.net Git - contagged/blob - xml.php
French language file
[contagged] / xml.php
1 <?php
2 ###################################################################################
3 #
4 # XML Library, by Keith Devens, version 1.0
5 # http://keithdevens.com/software/phpxml
6 #
7 # This code is Open Source, released under terms similar to the Artistic License.
8 # Read the license at http://keithdevens.com/software/license
9 #
10 ###################################################################################
11
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();
20         return $data;
21 }
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
28         $xml_parts = '';
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
34
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"]);
40                         }
41
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);
45                         }else{
46                                 if($prior_key) $key = $prior_key;
47                                 #(i.e. if we're in a numeric array, replace the number with the actual tag)
48
49                                 $xml_parts .= str_repeat("\t", $level).'<'.$key;
50                                 if($attributes) $xml_parts .= ' '.join(' ',$attributes);
51
52                                 if(is_null($value))
53                                         $xml_parts .= " />\r\n";
54                                 elseif(!is_array($value))
55                                         $xml_parts .= '>'.htmlspecialchars($value)."</$key>\r\n";
56                                 else
57                                         $xml_parts .= ">\r\n".XML_serialize($value, $level+1).str_repeat("\t", $level)."</$key>\r\n";
58                         }
59                 }
60         }
61         reset($data);
62         if($level == 0) return "<?xml version=\"1.0\" ?>\r\n".$xml_parts;
63         return $xml_parts;
64 }
65 ###################################################################################
66 # XML class: utility class to be used with PHP's XML handling functions
67 ###################################################################################
68 class XML {
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.
74
75         function XML(){
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');
81         }
82         function destruct(){
83                 xml_parser_free(&$this->parser);
84         }
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;
90         }
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]);
98                         }else{
99                                 #this is the second instance of $tag that we've seen
100                                 #shift around
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"]);
103                                 }else{
104                                         $arr = array(&$this->parent[$tag]);
105                                 }
106                                 $this->parent[$tag] = &$arr;
107                                 $key = 1;
108                         }
109                         $this->parent = &$this->parent[$tag];
110                 }else{
111                         $key = $tag;
112                 }
113                 if($attributes)
114                         $this->parent["$key attr"] = $attributes;
115
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;
119         }
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;
123         }
124         function close(&$parser, $tag){
125                 static $just_closed = false;
126                 if($this->last_opened_tag == $tag){
127                         if($this->data)
128                                 $this->parent = join('',$this->data);
129                         $this->last_opened_tag = NULL;
130                 }
131                 array_pop($this->stack);
132                 if($this->stack)
133                         $this->parent = &$this->stack[count($this->stack)-1];
134         }
135 }
136 function count_numeric_items(&$array){
137         return is_array($array) ? count(array_filter(array_keys($array), 'is_numeric')) : 0;
138 }
139 ?>