Affiliate Marketing
Forum Search

Reply
 
LinkBack Thread Tools Display Modes

  #1 (permalink)  
Old 19-01-06
morleymouse's Avatar
Super Member
 
Join Date: Aug 2003
Location: Costa Del Sheffield
Posts: 2,744
Thanks: 1
Thanked 10 Times in 6 Posts
morleymouse is an unknown quantity at this point
  bit of a pickle, php xml parser

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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 19-01-06
drivetowin's Avatar
Driving to win
 
Join Date: Aug 2003
Location: If I'm not at home, I'm in hospital
Posts: 7,349
Thanks: 4
Thanked 7 Times in 4 Posts
drivetowin seems to know their stuff
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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 19-01-06
morleymouse's Avatar
Super Member
 
Join Date: Aug 2003
Location: Costa Del Sheffield
Posts: 2,744
Thanks: 1
Thanked 10 Times in 6 Posts
morleymouse is an unknown quantity at this point
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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 19-01-06
Registered User
 
Join Date: Mar 2004
Location: Reading, UK
Posts: 301
Thanks: 0
Thanked 0 Times in 0 Posts
dmorison is an unknown quantity at this point
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! )
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 20-01-06
morleymouse's Avatar
Super Member
 
Join Date: Aug 2003
Location: Costa Del Sheffield
Posts: 2,744
Thanks: 1
Thanked 10 Times in 6 Posts
morleymouse is an unknown quantity at this point
  yes!

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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 20-01-06
km8 km8 is offline
Registered User
 
Join Date: May 2004
Location: Gondor
Posts: 1,196
Thanks: 0
Thanked 0 Times in 0 Posts
km8 is an unknown quantity at this point
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="";
}
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
xml > csv into mysql with a sprinkle of php morleymouse Widgets, Coding, AJAX, PHP - Technology & Affiliate Marketing 4 08-12-04 03:13 PM
Anyone anygood with xml, php, mysql and Affiliate Window AnnonnyMouse The Affiliate Marketing Lounge 1 23-09-04 11:00 AM
Transforming TD XML feeds with a stylesheet in PHP Andy TradeDoubler 0 12-08-04 03:32 PM
A bit of PHP help uklejon Widgets, Coding, AJAX, PHP - Technology & Affiliate Marketing 2 27-04-04 11:29 AM
PHP XML Parser morleymouse Widgets, Coding, AJAX, PHP - Technology & Affiliate Marketing 3 15-12-03 11:06 PM


Affiliate Marketing RSS Feeds - Contact Us - Affiliate Marketing - Archive - Privacy Statement - Top

Content Relevant URLs by vBSEO 3.2.0 RC7