Results 1 to 6 of 6

 

Thread: bit of a pickle, php xml parser

  1. #1
    Super Member

    Status
    Offline
    Join Date
    Aug 2003
    Location
    Costa Del Sheffield
    Posts
    2,838
    Thanks
    5
    Thanked 18 Times in 14 Posts


    im not too up on the whole xml / php parser type stuff but cobbled this together to parse some XML fro ma variable ($xml)

    PHP Code:
    <?
    $xml 
    "all my xml data here - it comes from a database";

    $parser xml_parser_create(  ); 
    xml_parser_set_option($parserXML_OPTION_CASE_FOLDING0);
    xml_parse_into_struct($parser,$xml,$values,$index); 
    xml_parser_free($parser);
            
    for(
    $i 0$i <= 200$i++) { 
            
        
    $desc $values[$index[desc][$i]][value];
        
    $title $values[$index[title][$i]][value];
        
    $url $values[$index[infourl][$i]][value];
    //    $type = $values[$index[type][$i]][value];

    if ($title): ?>

    I can then print out my content here
    <? echo $desc?> etc etc from above.

    <?
    endif;
    }
    ?>
    Quick explanation behind it, the whole $i up to 200 thing, its because I know there wont ever be more than 200 items, the if($title) makes sure I dont echo blank entries onto the page.

    I dont actually know what:

    PHP Code:
    $parser xml_parser_create(  ); 
    xml_parser_set_option($parserXML_OPTION_CASE_FOLDING0);
    xml_parse_into_struct($parser,$xml,$values,$index); 
    xml_parser_free($parser); 
    does so if anyone fancied explaining that it'd be nice!


    the problem...
    the $type (commented out i nthe above script) messes it all up. Some of the xml entries have a <type>something</type> but others dont have any <type> tags at all. So when the script runs through it it seems to move the types up. So if the 12th item has a type of say "green" it moves it up and the first item shows as having a type "green" and if no other tpyes are i nthe xml then item 12 whic hshould be green has no type with it.

    kind of complicated, I know how to fix it but dont know how to FIX it.

    I need the script to check each section of data for the tag and if it doesnt exist leave it blank or something like that?

    sample xml data for this...

    Code:
    <xmldata>
    <data>
    <desc></desc>
    <url></url>
    <type></type>   (not always there)
    </data>
    <data>
    <desc></desc>
    <url></url>
                             (see gone)
    </data>
    </xmldata>
    sorry if ive made this a bit lengthy but would be ever so greatful if anyone could help!
    Dan Morley
    alpharooms.com
    daniel at alpharooms dot com - Hotels, Flights, Airport Transfers, Care Hire + More! sign up
    My Blog | Cheap Holidays

  2. #2
    Driving to win

    Status
    Offline
    Join Date
    Aug 2003
    Location
    If I'm not at home, I'm in hospital
    Posts
    7,370
    Thanks
    7
    Thanked 16 Times in 11 Posts
    Don't know if this will work but have you tried

    Code:
    if ($values[$index[type][$i]][value]){
    $type = $values[$index[type][$i]][value];
    }
    Never argue with idiots. They just drag you down to their level and then beat you with their experience.

    If ignorance is bliss then some of the people I know must be orgasmic.

  3. #3
    Super Member

    Status
    Offline
    Join Date
    Aug 2003
    Location
    Costa Del Sheffield
    Posts
    2,838
    Thanks
    5
    Thanked 18 Times in 14 Posts
    ill give it a shot cheers, but I have a sneeky suspicion that it wont...

    what I thin kthe script is doing is just going throug hall the data until it finds the type tag and using it, doesnt matter which <data></data> it comes from, so...

    PHP Code:
    if ($values[$index[type][$i]][value]) 
    would be true, as it finds a type tag somewhere i nthe data and then it would use that again

    might be wrong like I said ill try it but thats my sneeking suspicion


    edit: cant check it now, changed some stuff earlier and dont have access from home to get them back. grrr ill be back tomorrow no doubt!
    Dan Morley
    alpharooms.com
    daniel at alpharooms dot com - Hotels, Flights, Airport Transfers, Care Hire + More! sign up
    My Blog | Cheap Holidays

  4. #4
    Registered User

    Status
    Offline
    Join Date
    Mar 2004
    Location
    Stafford, UK
    Posts
    323
    Thanks
    1
    Thanked 4 Times in 2 Posts
    Hi Dan,

    When I first started using PHP's XML functions a few years ago I had a look at parse_into_struct and my head nearly blew up!

    I tend to use the event handler method of parsing; as you have complete control over the data structure that you are creating. Here's something that should do what you want - a quick tested knock up! (hooray for "php -a" !!)

    PHP Code:
    <?php

    // this is your sample XML with some fields missing in each record

    $xml "
    <xmldata>
      <data><desc>Foo</desc><url>someurl</url><type>TEST</type></data>
      <data><desc>Bar</desc><url>someotherurl</url></data>
    </xmldata>
    "
    ;

    // start an array to parse the records into

    $records = array();

    // create a function to handle the opening tags in the XML
    function xmlStartTag($parser$name$attribs)
    {
      global 
    $currentRecord;  
      global 
    $currentTag;
      global 
    $inRecord;  
      if (
    $name == "DATA"// each of your records starts with <data> so start a new $currentRecord
      
    {
        
    $inRecord true;      
        
    $currentRecord = array();
      }  
      if (
    $inRecord)
      {
        
    $currentTag $name;
      }
    }
    // create a function to handle the data between tags
    function xmlCDATA($parser$cdata)
    {
      global 
    $currentRecord;  
      global 
    $currentTag;    
      
    $currentRecord[$currentTag] .= $cdata;
    }
    // create a function to handle the closing tags in the XML
    function xmlEndTag($parser$name)
    {
      global 
    $records;
      global 
    $currentRecord;
      global 
    $inRecord;  
      if (
    $name == "DATA"// ditto, this means we've reached the end, so add the current record to the $record array :)
      
    {
        
    $inRecord false;
        
    $records[] = $currentRecord;
      }
    }

    // create an XML parser resource
    $parser xml_parser_create(); 
    // register the functions to handle the beginning and end of each tag
    xml_set_element_handler($parser"xmlStartTag""xmlEndTag");
    // register a function to handle the data between tags (cdata)
    xml_set_character_data_handler($parser"xmlCDATA");
    // parse your string, the true value means there's no more data to come
    xml_parse($parser,$xml,true); 
    // free it up
    xml_parser_free($parser);

    // now you've parsed the records into $records have a look using print_r()
    print_r($records);

    ?>
    You could then go on to traverse the $records array using foreach, and the print out each of the expected parameters after testing to see if they are set, just as you are doing now:

    PHP Code:
    foreach($records as $data)
    {
      print 
    $data["DESC"]."<br>";
      print 
    $data["URL"]."<br>";
      if (
    $data["TYPE"]) print $data["TYPE"]."<br>";

    Hope this helps! (i've lived and breathed XML for the past 2 years as you know! )
    Developer of the Price Tapestry Price Comparison Script now with full WordPress Plugin!

  5. #5
    Super Member

    Status
    Offline
    Join Date
    Aug 2003
    Location
    Costa Del Sheffield
    Posts
    2,838
    Thanks
    5
    Thanked 18 Times in 14 Posts
    thank you loads!
    Dan Morley
    alpharooms.com
    daniel at alpharooms dot com - Hotels, Flights, Airport Transfers, Care Hire + More! sign up
    My Blog | Cheap Holidays

  6. #6
    km8
    Registered User

    Status
    Offline
    Join Date
    May 2004
    Location
    Gondor
    Posts
    1,202
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by kbudden
    Don't know if this will work but have you tried

    Code:
    if ($values[$index[type][$i]][value]){
    $type = $values[$index[type][$i]][value];
    }
    I think the problem is that $type is set from the previous run through the loop.

    Try this - should set $type to nothing if it doesn't exist for this item.

    Code:
    if ($values[$index[type][$i]][value]){
    $type = $values[$index[type][$i]][value];
    }
    else {
    $type="";
    }



Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. xml > csv into mysql with a sprinkle of php
    By morleymouse in forum Programming
    Replies: 4
    Last Post: 08-12-04, 02:13 PM
  2. Anyone anygood with xml, php, mysql and Affiliate Window
    By AnnonnyMouse in forum Affiliate Marketing Lounge
    Replies: 1
    Last Post: 23-09-04, 11:00 AM
  3. Replies: 0
    Last Post: 12-08-04, 03:32 PM
  4. A bit of PHP help
    By uklejon in forum Programming
    Replies: 2
    Last Post: 27-04-04, 11:29 AM
  5. PHP XML Parser
    By morleymouse in forum Programming
    Replies: 3
    Last Post: 15-12-03, 10:06 PM

Bookmarks

Posting Permissions

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

Content Relevant URLs by vBSEO 3.5.0 RC2