Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    me'
    me' is offline
    Senior Coder
    Join Date
    Nov 2002
    Location
    Warwickshire, England
    Posts
    1,229
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Problem with XML parser clearing variables

    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:
    Code:
      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:
    Code:
      $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.
    David House - Perfect is achieved, not when there is nothing left to add, but when there is nothing left to take away. (Antoine de St. Exupery).
    W3Schools | XHTML Validator | CSS Validator | Colours | Typography | HTML&CSS FAQ | Go get Mozilla Now | I blog!

  • #2
    me'
    me' is offline
    Senior Coder
    Join Date
    Nov 2002
    Location
    Warwickshire, England
    Posts
    1,229
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Oops, forgot to attach.
    Attached Files Attached Files
    David House - Perfect is achieved, not when there is nothing left to add, but when there is nothing left to take away. (Antoine de St. Exupery).
    W3Schools | XHTML Validator | CSS Validator | Colours | Typography | HTML&CSS FAQ | Go get Mozilla Now | I blog!

  • #3
    Senior Coder
    Join Date
    Jun 2002
    Location
    near Oswestry
    Posts
    4,508
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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 )
    Last edited by brothercake; 12-23-2003 at 11:32 AM.
    "Why bother with accessibility? ... Because deep down you know that the web is attractive to people who aren't exactly like you." - Joe Clark

  • #4
    me'
    me' is offline
    Senior Coder
    Join Date
    Nov 2002
    Location
    Warwickshire, England
    Posts
    1,229
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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).
    David House - Perfect is achieved, not when there is nothing left to add, but when there is nothing left to take away. (Antoine de St. Exupery).
    W3Schools | XHTML Validator | CSS Validator | Colours | Typography | HTML&CSS FAQ | Go get Mozilla Now | I blog!

  • #5
    Senior Coder
    Join Date
    Jun 2002
    Location
    near Oswestry
    Posts
    4,508
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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...t-tutorial.htm for the latest build or http://shanx.com/php/xsl/getXsl.htm for a PHP version earlier than 4.1
    "Why bother with accessibility? ... Because deep down you know that the web is attractive to people who aren't exactly like you." - Joe Clark

  • #6
    me'
    me' is offline
    Senior Coder
    Join Date
    Nov 2002
    Location
    Warwickshire, England
    Posts
    1,229
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Didn't really understand those pages. What does it do?
    David House - Perfect is achieved, not when there is nothing left to add, but when there is nothing left to take away. (Antoine de St. Exupery).
    W3Schools | XHTML Validator | CSS Validator | Colours | Typography | HTML&CSS FAQ | Go get Mozilla Now | I blog!

  • #7
    Senior Coder
    Join Date
    Jun 2002
    Location
    near Oswestry
    Posts
    4,508
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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
    Last edited by brothercake; 12-23-2003 at 12:31 PM.
    "Why bother with accessibility? ... Because deep down you know that the web is attractive to people who aren't exactly like you." - Joe Clark

  • #8
    me'
    me' is offline
    Senior Coder
    Join Date
    Nov 2002
    Location
    Warwickshire, England
    Posts
    1,229
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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?
    David House - Perfect is achieved, not when there is nothing left to add, but when there is nothing left to take away. (Antoine de St. Exupery).
    W3Schools | XHTML Validator | CSS Validator | Colours | Typography | HTML&CSS FAQ | Go get Mozilla Now | I blog!

  • #9
    Mega-ultimate member
    Join Date
    Jun 2002
    Location
    Winona, MN - The land of 10,000 lakes
    Posts
    1,855
    Thanks
    1
    Thanked 45 Times in 42 Posts
    Looks like your parsing RSS, have you checked out this thread?


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •