1 | <?php
|
---|
2 | ###################################################################################
|
---|
3 | #
|
---|
4 | # XML Library, by Keith Devens, version 1.2b
|
---|
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 | if( ! function_exists( 'xpress_XML_unserialize' ) ) : // d3pipe Used function
|
---|
17 | function & xpress_XML_unserialize(&$xml){
|
---|
18 | $xml_parser = &new xpress_XML();
|
---|
19 | $data = &$xml_parser->parse($xml);
|
---|
20 | $xml_parser->destruct();
|
---|
21 | return $data;
|
---|
22 | }
|
---|
23 | endif;
|
---|
24 | ###################################################################################
|
---|
25 | # XML_serialize: serializes any PHP data structure into XML
|
---|
26 | # Takes one parameter: the data to serialize. Must be an array.
|
---|
27 | ###################################################################################
|
---|
28 | if( ! function_exists( 'xpress_XML_serialize' ) ) : // d3pipe Used function
|
---|
29 | function & xpress_XML_serialize(&$data, $level = 0, $prior_key = NULL){
|
---|
30 | if($level == 0){ ob_start(); echo '<?xml version="1.0" ?>',"\n"; }
|
---|
31 | while(list($key, $value) = each($data))
|
---|
32 | if(!strpos($key, ' attr')) #if it's not an attribute
|
---|
33 | #we don't treat attributes by themselves, so for an empty element
|
---|
34 | # that has attributes you still need to set the element to NULL
|
---|
35 |
|
---|
36 | if(is_array($value) and array_key_exists(0, $value)){
|
---|
37 | xpress_XML_serialize($value, $level, $key);
|
---|
38 | }else{
|
---|
39 | $tag = $prior_key ? $prior_key : $key;
|
---|
40 | echo str_repeat("\t", $level),'<',$tag;
|
---|
41 | if(array_key_exists("$key attr", $data)){ #if there's an attribute for this element
|
---|
42 | while(list($attr_name, $attr_value) = each($data["$key attr"]))
|
---|
43 | echo ' ',$attr_name,'="',htmlspecialchars($attr_value),'"';
|
---|
44 | reset($data["$key attr"]);
|
---|
45 | }
|
---|
46 |
|
---|
47 | if(is_null($value)) echo " />\n";
|
---|
48 | elseif(!is_array($value)) echo '>',htmlspecialchars($value),"</$tag>\n";
|
---|
49 | else echo ">\n",xpress_XML_serialize($value, $level+1),str_repeat("\t", $level),"</$tag>\n";
|
---|
50 | }
|
---|
51 | reset($data);
|
---|
52 | if($level == 0){ $str = &ob_get_contents(); ob_end_clean(); return $str; }
|
---|
53 | }
|
---|
54 | endif;
|
---|
55 | ###################################################################################
|
---|
56 | # XML class: utility class to be used with PHP's XML handling functions
|
---|
57 | ###################################################################################
|
---|
58 | class xpress_XML{
|
---|
59 | var $parser; #a reference to the XML parser
|
---|
60 | var $document; #the entire XML structure built up so far
|
---|
61 | var $parent; #a pointer to the current parent - the parent will be an array
|
---|
62 | var $stack; #a stack of the most recent parent at each nesting level
|
---|
63 | var $last_opened_tag; #keeps track of the last tag opened.
|
---|
64 |
|
---|
65 | function xpress_XML(){
|
---|
66 | // $this->parser = &xml_parser_create();
|
---|
67 | $this->parser = xml_parser_create(); // GIJ
|
---|
68 | /*
|
---|
69 | xml_parser_set_option(&$this->parser, XML_OPTION_CASE_FOLDING, false);
|
---|
70 | xml_set_object(&$this->parser, &$this);
|
---|
71 | xml_set_element_handler(&$this->parser, 'open','close');
|
---|
72 | xml_set_character_data_handler(&$this->parser, 'data');
|
---|
73 | */
|
---|
74 | xml_parser_set_option($this->parser, XML_OPTION_CASE_FOLDING, false);
|
---|
75 | xml_set_object($this->parser, $this);
|
---|
76 | xml_set_element_handler($this->parser, 'open','close');
|
---|
77 | xml_set_character_data_handler($this->parser, 'data');
|
---|
78 | }
|
---|
79 | function destruct(){
|
---|
80 | // xml_parser_free(&$this->parser); // GIJ
|
---|
81 | xml_parser_free($this->parser);
|
---|
82 | }
|
---|
83 | function & parse(&$data){
|
---|
84 | $this->document = array();
|
---|
85 | $this->stack = array();
|
---|
86 | $this->parent = &$this->document;
|
---|
87 |
|
---|
88 | //libxml2 ver.2.7.0 -2.7.2 stripping leading angle brackets bug patch
|
---|
89 | if (
|
---|
90 | $GLOBALS['DO_LIBXML_PATCH'] == '1'
|
---|
91 | || LIBXML_DOTTED_VERSION == '2.7.0'
|
---|
92 | || LIBXML_DOTTED_VERSION == '2.7.1'
|
---|
93 | || LIBXML_DOTTED_VERSION == '2.7.2'
|
---|
94 | || (
|
---|
95 | LIBXML_DOTTED_VERSION == '2.7.3'
|
---|
96 | && version_compare( PHP_VERSION, '5.2.9', '<' )
|
---|
97 | )
|
---|
98 | ) {
|
---|
99 | $data =str_replace('<','<',$data );
|
---|
100 | $data =str_replace('>','>',$data );
|
---|
101 | $data =str_replace('&','&',$data );
|
---|
102 | }
|
---|
103 | //end Fix
|
---|
104 |
|
---|
105 | // return xml_parse(&$this->parser, &$data, true) ? $this->document : NULL; // GIJ
|
---|
106 | $ret = @xml_parse($this->parser, $data, true) ? $this->document : NULL;
|
---|
107 | return $ret ;
|
---|
108 | }
|
---|
109 | function open(&$parser, $tag, $attributes){
|
---|
110 | $this->data = ''; #stores temporary cdata
|
---|
111 | $this->last_opened_tag = $tag;
|
---|
112 | if(is_array($this->parent) and array_key_exists($tag,$this->parent)){ #if you've seen this tag before
|
---|
113 | if(is_array($this->parent[$tag]) and array_key_exists(0,$this->parent[$tag])){ #if the keys are numeric
|
---|
114 | #this is the third or later instance of $tag we've come across
|
---|
115 | $key = xpress_count_numeric_items($this->parent[$tag]);
|
---|
116 | }else{
|
---|
117 | #this is the second instance of $tag that we've seen. shift around
|
---|
118 | if(array_key_exists("$tag attr",$this->parent)){
|
---|
119 | $arr = array('0 attr'=>&$this->parent["$tag attr"], &$this->parent[$tag]);
|
---|
120 | unset($this->parent["$tag attr"]);
|
---|
121 | }else{
|
---|
122 | $arr = array(&$this->parent[$tag]);
|
---|
123 | }
|
---|
124 | $this->parent[$tag] = &$arr;
|
---|
125 | $key = 1;
|
---|
126 | }
|
---|
127 | $this->parent = &$this->parent[$tag];
|
---|
128 | }else{
|
---|
129 | $key = $tag;
|
---|
130 | }
|
---|
131 | if($attributes) $this->parent["$key attr"] = $attributes;
|
---|
132 | $this->parent = &$this->parent[$key];
|
---|
133 | $this->stack[] = &$this->parent;
|
---|
134 | }
|
---|
135 | function data(&$parser, $data){
|
---|
136 | if($this->last_opened_tag != NULL) #you don't need to store whitespace in between tags
|
---|
137 | $this->data .= $data;
|
---|
138 | }
|
---|
139 | function close(&$parser, $tag){
|
---|
140 | if($this->last_opened_tag == $tag){
|
---|
141 | $this->parent = $this->data;
|
---|
142 | $this->last_opened_tag = NULL;
|
---|
143 | }
|
---|
144 | array_pop($this->stack);
|
---|
145 | if($this->stack) $this->parent = &$this->stack[count($this->stack)-1];
|
---|
146 | }
|
---|
147 | }
|
---|
148 |
|
---|
149 | if( ! function_exists( 'xpress_count_numeric_items' ) ) : // d3pipe Used function
|
---|
150 | function xpress_count_numeric_items(&$array){
|
---|
151 | return is_array($array) ? count(array_filter(array_keys($array), 'is_numeric')) : 0;
|
---|
152 | }
|
---|
153 | endif;
|
---|
154 | ?> |
---|