View Full Version : Problem with XML parser clearing variables

12-22-2003, 11:31 PM
I'm trying to build a simple XML parser for use of experience only. I've thought my algorithm through, and I'm sure it should work. The problem is, the xml_parse() function seems to clear my global variables after each run through. Any idea why this is doing this? Opening tag handler:
function opener($parser, $element, $attributes) {
if ($cThing != "") $parent = $cThing;
$cThing = $element;
switch ($parent) {
case "": $channels[$ccount]["items"] = array(); break;
case "channel": if ($cThing == "item" || $cThing == "image")
$channels[$ccount]["items"][$cThing] = array(); break;
case "item": $channels[$ccount]["items"][$icount] = array(); break;
case "image": $channels[$ccount]["image"] = array(); break;
}I'm parsing it like this:
$parser = xml_parser_create();
xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, false);
xml_set_element_handler($parser, 'opener', 'closer');
xml_set_character_data_handler($parser, 'data');

foreach ($feeds as $currFeed) { //foreach feed
$thisFeed = file(rtrim($currFeed));
foreach ($thisFeed as $line) { //foreach line in each feed
xml_parse($parser, $line);
}My closing tag handler just clears $cThing. There's no problem with my data handler. $icount, $ccount, $parent, $feeds and $cThing are declared right at the very top of my file. I'll attach it for anyone that wants a clearer look.

12-22-2003, 11:31 PM
Oops, forgot to attach.

12-23-2003, 11:29 AM
I'm a bit rusty with expat, but if memory serves the order of the function calls is significant - it doesn't run through the tree and process the nodes in order, it process all the open tags, then all the PCDATA, then all the close tags.

To get it to work I used the globals array to store data from each pass.

$GLOBALS["foo"] = "bar";

Don't know if that helps, like I say I'm a little rusty.

(Can you get new extensions installed on your server? XSLT is much easier, and more logical :))

12-23-2003, 11:32 AM
Originally posted by brothercake
(Can you get new extensions installed on your server? XSLT is much easier, and more logical :)) Actually, I've installed apache on my local PC and that's where it's running. I think I'll give this up, and go for my CMS plan (XHTML page with a form that uses PHP to produce a RSS file, which I'll use XSLT to parse and PHP to include the results back into my blog page).

12-23-2003, 11:36 AM
All you need is the Sablotron extensions - http://www.gingerall.com/charlie/ga/xml/p_sab.xml - which are easy to install on Windows/Apache if you have good instructions - http://sniptools.com/vault/sablotron-xslt-php-apache-on-windows-and-a-basic-xslt-tutorial.htm for the latest build or http://shanx.com/php/xsl/getXsl.htm for a PHP version earlier than 4.1

12-23-2003, 11:41 AM
Didn't really understand those pages. What does it do?

12-23-2003, 12:26 PM
It's an XSLT processor, so you can do XSL with PHP. That's just the installation and setup information though. For actually using it, look at the PHP manual - http://www.php.net/manual/en/ref.xslt.php

12-23-2003, 07:29 PM
Okay, I installed the Sablotron dll to my <root to php>/dlls/ directory (although I'm not sure it was essential, phpdev seems to have come with it). expat.dll was already there . When I try to use it I get a 'Call to undefined function' fatal error. Any insight? Any requires I've missed?

12-23-2003, 07:35 PM
Looks like your parsing RSS, have you checked out this (http://www.codingforums.com/showthread.php?s=&threadid=26012) thread?