| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * DomXml object |
| 5 | */ |
| 6 | class DomXml { |
| 7 | |
| 8 | var $dom; |
| 9 | |
| 10 | function DomXml($xml_data) { |
| 11 | if (@is_file($xml_data)) { |
| 12 | if (! $this->dom = domxml_open_file($xml_data)) { |
| 13 | //echo "Error while parsing the document.\n"; |
| 14 | unset($this->dom); |
| 15 | return FALSE; |
| 16 | } |
| 17 | } else { |
| 18 | if (! $this->dom = domxml_open_mem($xml_data)) { |
| 19 | echo "<!-- Error while parsing the document. -->\n"; |
| 20 | unset($this->dom); |
| 21 | return FALSE; |
| 22 | } |
| 23 | } |
| 24 | |
| 25 | return TRUE; |
| 26 | } |
| 27 | |
| 28 | /** |
| 29 | * root element |
| 30 | */ |
| 31 | function getRoot() { |
| 32 | if (empty($this->dom)) { |
| 33 | return FALSE; |
| 34 | } else { |
| 35 | $root = new DomXmlNode($this->dom->document_element()); |
| 36 | $nodes = new DomXmlNodes($root); |
| 37 | return $nodes; |
| 38 | |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * get nodes |
| 44 | */ |
| 45 | function getNodes($tag_name) { |
| 46 | $root = DomXml::getRoot(); |
| 47 | if ($root) { |
| 48 | return $root->getNodes("$tag_name"); |
| 49 | |
| 50 | } else { |
| 51 | return FALSE; |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * get node |
| 57 | */ |
| 58 | function getNode($tag_name) { |
| 59 | $nodes = DomXml::getNodes($tag_name); |
| 60 | if ($nodes) { |
| 61 | return $nodes->next(); |
| 62 | } else { |
| 63 | return FALSE; |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * get node content |
| 69 | */ |
| 70 | function getNodeContent($tag_name) { |
| 71 | $node = DomXml::getNode($tag_name); |
| 72 | if ($node) { |
| 73 | return $node->getContent(); |
| 74 | } |
| 75 | return ''; |
| 76 | } |
| 77 | |
| 78 | function get($tag_name) { |
| 79 | return DomXml::getNodeContent($tag_name); |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * DomXmlNodes object |
| 85 | */ |
| 86 | class DomXmlNodes { |
| 87 | var $nodes = array(); |
| 88 | var $current_node = -1; |
| 89 | |
| 90 | function DomXmlNodes($nodes) { |
| 91 | if (is_array($nodes)) { |
| 92 | $this->nodes = $nodes; |
| 93 | } else { |
| 94 | $this->nodes = array($nodes); |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * next |
| 100 | */ |
| 101 | function next() { |
| 102 | if ($this->current_node + 1 >= count($this->nodes)) { |
| 103 | return FALSE; |
| 104 | } |
| 105 | $this->current_node += 1; |
| 106 | return $this->nodes[$this->current_node]; |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * rewind |
| 111 | */ |
| 112 | function rewind() { |
| 113 | $this->current_node = -1; |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * get nodes |
| 118 | */ |
| 119 | function getNodes($node_name) { |
| 120 | $node_names = explode("/",$node_name); |
| 121 | $last_node_name = array_pop($node_names); |
| 122 | |
| 123 | $node = $this->nodes[0]; |
| 124 | print_r($this->nodes[1]); |
| 125 | while ($node_name = array_shift($node_names)) { |
| 126 | $found_node = $node->findNode($node_name); |
| 127 | if (! $found_node) { |
| 128 | return new DomXmlNodes(array()); |
| 129 | } |
| 130 | $node = new DomXmlNode($found_node); |
| 131 | } |
| 132 | $found_node = $node->findNodes($last_node_name); |
| 133 | if ($found_node) { |
| 134 | return new DomXmlNodes($found_node); |
| 135 | } else { |
| 136 | return new DomXmlNodes(array()); |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * get node |
| 142 | */ |
| 143 | function getNode($node_name) { |
| 144 | $nodes = DomXmlNodes::getNodes($node_name); |
| 145 | if ($nodes) { |
| 146 | return $nodes->next(); |
| 147 | } |
| 148 | return FALSE; |
| 149 | } |
| 150 | |
| 151 | /** |
| 152 | * get node content |
| 153 | */ |
| 154 | function getNodeContent($tag_name) { |
| 155 | $node = DomXmlNodes::getNode(); |
| 156 | if ($node) { |
| 157 | return $node->getContent(); |
| 158 | } |
| 159 | return ''; |
| 160 | } |
| 161 | |
| 162 | function get($tag_name) { |
| 163 | return DomXml::getNodeContent($tag_name); |
| 164 | } |
| 165 | |
| 166 | function hasRows() { |
| 167 | return DomXmlNodes::getNumRows(); |
| 168 | } |
| 169 | |
| 170 | function getNumRows() { |
| 171 | return @count($this->nodes); |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | /** |
| 176 | * DomXmlNode object |
| 177 | */ |
| 178 | class DomXmlNode { |
| 179 | |
| 180 | var $node; |
| 181 | var $output_encoding = ''; |
| 182 | |
| 183 | function DomXmlNode($node,$output_encoding = 'windows-1250') { |
| 184 | $this->node = $node; |
| 185 | if ($output_encoding) { |
| 186 | $this->output_encoding = $output_encoding; |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | /** |
| 191 | * get node content |
| 192 | */ |
| 193 | function getContent() { |
| 194 | |
| 195 | if ($this->node && $this->node->has_child_nodes()) { |
| 196 | $node = array_shift($this->node->child_nodes()); |
| 197 | } else { |
| 198 | return ''; |
| 199 | } |
| 200 | |
| 201 | $content = $node->get_content(); |
| 202 | $content = str_replace("#parse_nl#"," ",$content); |
| 203 | $content = DomXmlNode::unhtmlspecialchars($content); |
| 204 | |
| 205 | return $content; |
| 206 | } |
| 207 | |
| 208 | /** |
| 209 | * content of subnode specified by node name |
| 210 | */ |
| 211 | function getNodeContent($node_name) { |
| 212 | $node_names = explode("/",$node_name); |
| 213 | $last_node_name = array_pop($node_names); |
| 214 | |
| 215 | $node = $this; |
| 216 | while ($node_name = array_shift($node_names)) { |
| 217 | $found_node = $node->findNode($node_name); |
| 218 | if (! $found_node) { |
| 219 | return FALSE; |
| 220 | } |
| 221 | $node = new DomXmlNode($found_node); |
| 222 | } |
| 223 | |
| 224 | if ($node = $node->findNode($last_node_name)) { |
| 225 | $node = new DomXmlNode($node); |
| 226 | return $node->getContent(); |
| 227 | } |
| 228 | return ''; |
| 229 | } |
| 230 | |
| 231 | function get($node_name='') { |
| 232 | if ($node_name) { |
| 233 | return DomXmlNode::getNodeContent($node_name); |
| 234 | } else { |
| 235 | return DomXmlNode::getContent(); |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | /** |
| 240 | * find node |
| 241 | */ |
| 242 | function findNode($node_name) { |
| 243 | $node = $this->node; |
| 244 | |
| 245 | if (! ($node || $node->has_child_node()) ) { |
| 246 | |
| 247 | return FALSE; |
| 248 | } |
| 249 | |
| 250 | $child = $node->first_child(); |
| 251 | |
| 252 | while ($child) { |
| 253 | if ($child->node_type() == XML_ELEMENT_NODE && $child->tagname() == $node_name) { |
| 254 | return $child; |
| 255 | } |
| 256 | $child = $child->next_sibling(); |
| 257 | } |
| 258 | |
| 259 | return FALSE; |
| 260 | } |
| 261 | |
| 262 | /** |
| 263 | * find nodes |
| 264 | */ |
| 265 | function findNodes($node_name) { |
| 266 | $nodes = array(); |
| 267 | |
| 268 | $node = $this->node; |
| 269 | |
| 270 | if (! ($node || $node->has_child_node()) ) { |
| 271 | return FALSE; |
| 272 | } |
| 273 | |
| 274 | $child = $node->first_child(); |
| 275 | |
| 276 | while ($child) { |
| 277 | if ($child->node_type() == XML_ELEMENT_NODE && $child->tagname() == $node_name) { |
| 278 | $nodes[] = new DomXmlNode($child); |
| 279 | } |
| 280 | $child = $child->next_sibling(); |
| 281 | } |
| 282 | |
| 283 | return $nodes; |
| 284 | } |
| 285 | |
| 286 | function unhtmlspecialchars($str) { |
| 287 | $html_special_chars_table = array( |
| 288 | "&" => "&", |
| 289 | "\"" => """, |
| 290 | "'" => "'", |
| 291 | "<" => "<", |
| 292 | ">" => ">", |
| 293 | "\t" => "	", |
| 294 | "\n" => " ", |
| 295 | "\r" => " " |
| 296 | ); |
| 297 | $html_special_chars_table = array_flip($html_special_chars_table); |
| 298 | |
| 299 | return strtr($str,$html_special_chars_table); |
| 300 | } |
| 301 | |
| 302 | } |
| 303 | |
| 304 | ?> |