Go to the documentation of this file.00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00033 class XMLStruct
00034 {
00035 var $childs = array();
00036 var $parent;
00037 var $name;
00038 var $content = array();
00039 var $attrs;
00040
00044 function XMLStruct($a_name = "", $a_attrs = array())
00045 {
00046 $this->name = $a_name;
00047 $this->attrs = $a_attrs;
00048 }
00049
00053 function append($a_name, $a_attrs)
00054 {
00055 $struct = new XMLStruct($a_name, $a_attrs);
00056 $struct->parent =& $GLOBALS["lastObj"];
00057
00058 $GLOBALS["lastObj"] =& $struct;
00059 $this->childs[] =& $struct;
00060 }
00061
00065 function setParent()
00066 {
00067 $GLOBALS["lastObj"] =& $GLOBALS["lastObj"]->parent;
00068 }
00069
00073 function setContent($a_data)
00074 {
00075
00076 $this->content[] = $a_data;
00077 }
00078
00084 function insert(&$dom, &$node)
00085 {
00086 $newNode = $dom->create_element($this->name);
00087 if ($this->content != "")
00088 {
00089 $newNode->set_content(implode("", $this->content));
00090 }
00091 if (is_array($this->attrs))
00092 {
00093 #vd($this->attrs);
00094 reset ($this->attrs);
00095 while (list ($key, $val) = each ($this->attrs)) {
00096 $newNode->set_attribute($key, $val);
00097 }
00098 }
00099 $node = $node->append_child($newNode);
00100 for ($j = 0; $j < count($this->childs); $j++)
00101 {
00102 $this->childs[$j]->insert($dom, $node);
00103 }
00104 $node = $node->parent_node();
00105 }
00106 }
00107
00108 class XML2DOM
00109 {
00110
00111 function XML2DOM($a_xml)
00112 {
00113 $xml_parser = xml_parser_create("UTF-8");
00114 xml_parser_set_option($xml_parser, XML_OPTION_CASE_FOLDING, false);
00115 xml_set_object($xml_parser, $this);
00116 xml_set_element_handler($xml_parser, "startElement", "endElement");
00117 xml_set_character_data_handler($xml_parser, "characterData");
00118
00119 if (!xml_parse($xml_parser, $a_xml, true))
00120 {
00121 die(sprintf("XML error: %s at line %d",
00122 xml_error_string(xml_get_error_code($xml_parser)),
00123 xml_get_current_line_number($xml_parser)));
00124 }
00125 xml_parser_free($xml_parser);
00126 }
00127
00128 function clean(&$attr)
00129 {
00130 if(is_array($attr))
00131 {
00132 foreach($attr as $key => $value)
00133 {
00134 $attr[$key] = preg_replace("/&(?!amp;|lt;|gt;|quot;)/","&",$attr[$key]);
00135 $attr[$key] = preg_replace("/\"/",""",$attr[$key]);
00136 $attr[$key] = preg_replace("/</","<",$attr[$key]);
00137 $attr[$key] = preg_replace("/>/",">",$attr[$key]);
00138 }
00139 }
00140 return $attr;
00141 }
00142
00143
00144 function startElement($a_parser, $a_name, $a_attrs)
00145 {
00146 if (!is_object($this->xmlStruct))
00147 {
00148 #vd($a_attrs);
00149 $this->xmlStruct = new XMLStruct($a_name, $a_attrs);
00150 $GLOBALS["lastObj"] =& $this->xmlStruct;
00151 }
00152 else
00153 {
00154 $a_attrs = $this->clean($a_attrs);
00155 #var_dump("<pre>",++$counter," ",$a_name," -> ",$a_attrs,"<pre>");
00156 $GLOBALS["lastObj"]->append($a_name, $a_attrs);
00157 }
00158 }
00159
00160 function endElement($a_parser, $a_name)
00161 {
00162 $GLOBALS["lastObj"]->setParent();
00163 }
00164
00165 function characterData($a_parser, $a_data)
00166 {
00167 $a_data = preg_replace("/&/","&",$a_data);
00168
00169 $GLOBALS["lastObj"]->setContent($a_data);
00170 }
00171
00172 function insertNode(&$dom, &$node)
00173 {
00174 $node = $this->xmlStruct->insert($dom, $node);
00175 }
00176 }
00177