note 59742 deleted from ref.xml by bjori
| From: | bjori@php.net | Date: | Mon, 09 Oct 2006 11:09:36 +0000 |
| Subject: | note 59742 deleted from ref.xml by bjori | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-118274@lists.php.net to get a copy of this message | ||
Note Submitter: orbitphreak at yahoo dot com
----
Here's another version of raphael's XMLParser class. The modification allows you to parse
a remote (HTTP) XML file into an array without the need of fopen or fsockopen, which are usually
disabled by hosting companies.
<?PHP
class XMLParser {
var $xml_url;
var $xml;
var $data;
function XMLParser($xml_url) {
$this->xml_url = $xml_url;
$this->xml = xml_parser_create();
xml_set_object($this->xml, $this);
xml_set_element_handler($this->xml, 'startHandler', 'endHandler');
xml_set_character_data_handler($this->xml, 'dataHandler');
$this->parse($xml_url);
}
function parse($xml_url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $xml_url);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
$store = curl_exec ($ch);
$data = curl_exec ($ch);
curl_close ($ch);
$parse = xml_parse($this->xml, $data, sizeof($data));
if (!$parse) {
die(sprintf("XML error: %s at line %d",
xml_error_string(xml_get_error_code($this->xml)),
xml_get_current_line_number($this->xml)));
xml_parser_free($this->xml
);
}
return true;
}
function startHandler($parser, $name, $attributes) {
$data['name'] = $name;
if ($attributes) { $data['attributes'] = $attributes; }
$this->data[] = $data;
}
function dataHandler($parser, $data) {
if ($data = trim($data)) {
$index = count($this->data) - 1;
// begin multi-line bug fix (use the .= operator)
$this->data[$index]['content'] .= $data;
// end multi-line bug fix
}
}
function endHandler($parser, $name) {
if (count($this->data) > 1) {
$data = array_pop($this->data);
$index = count($this->data) - 1;
$this->data[$index]['child'][] = $data;
}
}
}
?>
Examples on how to use it to parse out geographic data are here:
http://www.digital-seven.net/?option=com_content&task=view&id=69