Hi there,
I am having some trouble in parsing some XML. No idea where I have that xml
parser for php from, but I believe it worked ok in the past. Now I do have a
file to parse with following structure:
<SEARCHRESULTS>
<RESULTSET DATABASE>
<RESULT POS="1">
<TITLE>I want to grab this title</TITLE>
With my xml parser I do just get "RESULT = "
Could anybody please give me a hand to get this running. I am some how lost
since I do not have to do as much with XML so far.
Thank you in advance,
Merlin
Here comes my xml-parser.php code:
<?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;
}
}
}
$url = ("http://this ist the url with the xml file");
$myFile = new XMLParser($url);
$xmlRoot = $myFile->data[0]; // GEO Element
if (sizeof($xmlRoot) > 0 ) {
$geoLocation = $xmlRoot['child'];
if (sizeof($geoLocation) > 0) {
$geoLocationProperties = $geoLocation[0]['child'];
for ($i = 0 ; $i < sizeof($geoLocationProperties); $i++) {
echo $geoLocationProperties[$i]['name'] . " = " .
$geoLocationProperties[$i]['content']."<br/>";
}
}
}
?>
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php