Remove these advertisements by upgrading your account for only $5/month
001: <?php 002: /* 003: Source : http://pl2.php.net/manual/en/function.xml-parse.php#83416 004: */ 005: 006: class xx_xml { 007: 008: // XML parser variables 009: var $parser; 010: var $name; 011: var $attr; 012: var $data = array(); 013: var $stack = array(); 014: var $keys; 015: var $path; 016: 017: // either you pass url atau contents. 018: // Use 'url' or 'contents' for the parameter 019: var $type; 020: 021: // function with the default parameter value 022: function xx_xml($url='http://www.opocot.com', $type='url') { 023: $this->type = $type; 024: $this->url = $url; 025: $this->parse(); 026: } 027: 028: // parse XML data 029: function parse() 030: { 031: $data = ''; 032: $this->parser = xml_parser_create(); 033: xml_set_object($this->parser, $this); 034: xml_set_element_handler($this->parser, 'startXML', 'endXML'); 035: xml_set_character_data_handler($this->parser, 'charXML'); 036: 037: xml_parser_set_option($this->parser, XML_OPTION_CASE_FOLDING, false); 038: 039: if ($this->type == 'url') { 040: // if use type = 'url' now we open the XML with fopen 041: 042: if (!($fp = @fopen($this->url, 'rb'))) { 043: $this->error("Cannot open {$this->url}"); 044: } 045: 046: while (($data = fread($fp, 8192))) { 047: if (!xml_parse($this->parser, $data, feof($fp))) { 048: $this->error(sprintf('XML error at line %d column %d', 049: xml_get_current_line_number($this->parser), 050: xml_get_current_column_number($this->parser))); 051: } 052: } 053: } else if ($this->type == 'contents') { 054: // Now we can pass the contents, maybe if you want 055: // to use CURL, SOCK or other method. 056: $lines = explode("\n",$this->url); 057: foreach ($lines as $val) { 058: if (trim($val) == '') 059: continue; 060: $data = $val . "\n"; 061: if (!xml_parse($this->parser, $data)) { 062: $this->error(sprintf('XML error at line %d column %d', 063: xml_get_current_line_number($this->parser), 064: xml_get_current_column_number($this->parser))); 065: } 066: } 067: } 068: } 069: 070: function startXML($parser, $name, $attr) { 071: $this->stack[$name] = array(); 072: $keys = ''; 073: $total = count($this->stack)-1; 074: $i=0; 075: foreach ($this->stack as $key => $val) { 076: if (count($this->stack) > 1) { 077: if ($total == $i) 078: $keys .= $key; 079: else 080: $keys .= $key . '|'; // The saparator 081: } 082: else 083: $keys .= $key; 084: $i++; 085: } 086: if (array_key_exists($keys, $this->data)) { 087: $this->data[$keys][] = $attr; 088: } else 089: $this->data[$keys] = $attr; 090: $this->keys = $keys; 091: } 092: 093: function endXML($parser, $name) { 094: end($this->stack); 095: if (key($this->stack) == $name) 096: array_pop($this->stack); 097: } 098: 099: function charXML($parser, $data) { 100: if (trim($data) != '') 101: $this->data[$this->keys]['data'][] = trim(str_replace("\n", '', $data)); 102: } 103: 104: function error($msg) { 105: echo "<div align=\"center\"> 106: <font color=\"red\"><b>Error: $msg</b></font> 107: </div>"; 108: exit(); 109: } 110: } 111: 112: ?>
| Revision | Author | Commited | Message | |
|---|---|---|---|---|
| 2 |
|
Tue 17 Nov, 2009 18:34:43 +0000 | Blitter package upload. |
Remove these advertisements by upgrading your account for only $5/month