[35] | 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 | ###################################################################################
|
---|
[42] | 16 | if( ! function_exists( 'xpress_XML_unserialize' ) ) : // d3pipe Used function
|
---|
| 17 | function & xpress_XML_unserialize(&$xml){
|
---|
| 18 | $xml_parser = &new xpress_XML();
|
---|
[35] | 19 | $data = &$xml_parser->parse($xml);
|
---|
| 20 | $xml_parser->destruct();
|
---|
| 21 | return $data;
|
---|
| 22 | }
|
---|
[39] | 23 | endif;
|
---|
[35] | 24 | ###################################################################################
|
---|
| 25 | # XML_serialize: serializes any PHP data structure into XML
|
---|
| 26 | # Takes one parameter: the data to serialize. Must be an array.
|
---|
| 27 | ###################################################################################
|
---|
[42] | 28 | if( ! function_exists( 'xpress_XML_serialize' ) ) : // d3pipe Used function
|
---|
| 29 | function & xpress_XML_serialize(&$data, $level = 0, $prior_key = NULL){
|
---|
[35] | 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)){
|
---|
[42] | 37 | xpress_XML_serialize($value, $level, $key);
|
---|
[35] | 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";
|
---|
[42] | 49 | else echo ">\n",xpress_XML_serialize($value, $level+1),str_repeat("\t", $level),"</$tag>\n";
|
---|
[35] | 50 | }
|
---|
| 51 | reset($data);
|
---|
| 52 | if($level == 0){ $str = &ob_get_contents(); ob_end_clean(); return $str; }
|
---|
| 53 | }
|
---|
[39] | 54 | endif;
|
---|
[35] | 55 | ###################################################################################
|
---|
| 56 | # XML class: utility class to be used with PHP's XML handling functions
|
---|
| 57 | ###################################################################################
|
---|
[42] | 58 | class xpress_XML{
|
---|
[35] | 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 |
|
---|
[42] | 65 | function xpress_XML(){
|
---|
[39] | 66 | // $this->parser = &xml_parser_create();
|
---|
| 67 | $this->parser = xml_parser_create(); // GIJ
|
---|
| 68 | /*
|
---|
[35] | 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');
|
---|
[39] | 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');
|
---|
[35] | 78 | }
|
---|
[39] | 79 | function destruct(){
|
---|
| 80 | // xml_parser_free(&$this->parser); // GIJ
|
---|
| 81 | xml_parser_free($this->parser);
|
---|
| 82 | }
|
---|
[35] | 83 | function & parse(&$data){
|
---|
| 84 | $this->document = array();
|
---|
| 85 | $this->stack = array();
|
---|
| 86 | $this->parent = &$this->document;
|
---|
[39] | 87 | // return xml_parse(&$this->parser, &$data, true) ? $this->document : NULL; // GIJ
|
---|
| 88 | $ret = @xml_parse($this->parser, $data, true) ? $this->document : NULL;
|
---|
| 89 | return $ret ;
|
---|
[35] | 90 | }
|
---|
| 91 | function open(&$parser, $tag, $attributes){
|
---|
| 92 | $this->data = ''; #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
|
---|
[42] | 97 | $key = xpress_count_numeric_items($this->parent[$tag]);
|
---|
[35] | 98 | }else{
|
---|
| 99 | #this is the second instance of $tag that we've seen. shift around
|
---|
| 100 | if(array_key_exists("$tag attr",$this->parent)){
|
---|
| 101 | $arr = array('0 attr'=>&$this->parent["$tag attr"], &$this->parent[$tag]);
|
---|
| 102 | 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) $this->parent["$key attr"] = $attributes;
|
---|
| 114 | $this->parent = &$this->parent[$key];
|
---|
| 115 | $this->stack[] = &$this->parent;
|
---|
| 116 | }
|
---|
| 117 | function data(&$parser, $data){
|
---|
| 118 | if($this->last_opened_tag != NULL) #you don't need to store whitespace in between tags
|
---|
| 119 | $this->data .= $data;
|
---|
| 120 | }
|
---|
| 121 | function close(&$parser, $tag){
|
---|
| 122 | if($this->last_opened_tag == $tag){
|
---|
| 123 | $this->parent = $this->data;
|
---|
| 124 | $this->last_opened_tag = NULL;
|
---|
| 125 | }
|
---|
| 126 | array_pop($this->stack);
|
---|
| 127 | if($this->stack) $this->parent = &$this->stack[count($this->stack)-1];
|
---|
| 128 | }
|
---|
| 129 | }
|
---|
[40] | 130 |
|
---|
[42] | 131 | if( ! function_exists( 'xpress_count_numeric_items' ) ) : // d3pipe Used function
|
---|
| 132 | function xpress_count_numeric_items(&$array){
|
---|
[35] | 133 | return is_array($array) ? count(array_filter(array_keys($array), 'is_numeric')) : 0;
|
---|
| 134 | }
|
---|
[40] | 135 | endif;
|
---|
[35] | 136 | ?> |
---|