Jump to content

XML Parsing (Looping a specific nodes contents)


alwoodman

Recommended Posts

Hi I have an XML parser which works fine if the XML file is flat but i cant get it to work when i want to loop through one particular node. For example below there are 3 category ID's each with an unlimited number of events (EventsList) in each...i want to get these in my database but can only get the parser to loop through the top level

 

This is te XML format


<TopLevel>
<PrimaryCategory>
<PrimaryCategoryID></PrimaryCategoryID>
<PrimaryCategoryName></PrimaryCategoryName>
<PrimaryCategoryURL></PrimaryCategoryURL>
<CategoryList>
<CategoryID>1</CategoryID>
<CategoryName></CategoryName>
<CategoryURL></CategoryURL>
<CategoryImageURL></CategoryImageURL>
<CategoryDescription></CategoryDescription>
<EventList>
<EventID></EventID>
<EventName></EventName>
<EventDate></EventDate>
<EventURL></EventURL>
<VenueName></VenueName>
<VenueCity></VenueCity>
<VenueAddress></VenueAddress>
<VenueState></VenueState>
<VenueCountryCode></VenueCountryCode>
<VenuePostCode></VenuePostCode>
<MinCurrentPrice></MinCurrentPrice>
<MaxCurrentPrice></MaxCurrentPrice>
<TicketCount></TicketCount>
<PresaleDate></PresaleDate>
<OnSaleDate></OnSaleDate>
<SoldOutDate></SoldOutDate>
</EventList>
</CategoryList>
</PrimaryCategory>
</TopLevel>

 

This is my parser


$xmlFeed = "../../xml/events.xml";


$data = @implode("",file($xmlFeed));
$parser = xml_parser_create();
xml_parser_set_option($parser,XML_OPTION_SKIP_WHITE,1);
xml_parser_set_option($parser,XML_OPTION_CASE_FOLDING,0);
xml_parse_into_struct($parser,$data,$d_ar,$i_ar);
xml_parser_free($parser);

/**
* The XML data is broken up into the two arrays,
* $d_ar and $i_ar, now we can loop through
* the arrays to display the data on our website:
*/

for($i=0; $i<count($i_ar['CategoryList']); $i++) {

  if($d_ar[$i_ar['CategoryList'][$i]]['type']=='open') {
    for($j=$i_ar['CategoryList'][$i]; $j<$i_ar['CategoryList'][$i+1]; $j++) {



/**
* Assign the data to a variable;
* to include more tags, add extra elseif statements
*/
      if($d_ar[$j]['tag'] == 'EventID'){
  	$eventid = mysql_real_escape_string($d_ar[$j]['value']);

      }elseif($d_ar[$j]['tag'] == 'EventName'){
        $eventname = $d_ar[$j]['value'];

  }elseif($d_ar[$j]['tag'] == 'EventDate'){
  
	$date  = $d_ar[$j]['value'];;
	$datesplit = explode("T", $date);
	echo $datesplit[0]; // piece1
	echo $datesplit[1]; // piece2
  

      }elseif($d_ar[$j]['tag'] == 'EventURL'){
        $eventurl = $d_ar[$j]['value'];

  }elseif($d_ar[$j]['tag'] == 'VenueCity'){
        $venuecity = $d_ar[$j]['value'];

      }elseif($d_ar[$j]['tag'] == 'MinCurrentPrice'){
        $mincurrentprice = $d_ar[$j]['value'];

	}elseif($d_ar[$j]['tag'] == 'CategoryDescription'){
        $categorydescription = $d_ar[$j]['value'];
  
  
    }

// --- Build the rows ---

		mysql_select_db($database_system_conn, $system_conn);
		$query_doEventCheck = "SELECT * FROM tbl_events WHERE event_id = $eventid";
		$doEventCheck = mysql_query($query_doEventCheck, $system_conn) or die(mysql_error());
		$row_doEventCheck = mysql_fetch_assoc($doEventCheck);
		$totalRows_doEventCheck = mysql_num_rows($doEventCheck);



///////////////////////////////////////////START INITIAL INCLUDE IF DOESNT EXIST
		if ($totalRows_doEventCheck == 0) {

		$insertSQL = "

		INSERT INTO `tbl_events` ( `event_id` , `event_genID` , `event_name` , `event_category` , `event_desc` , `event_price` , `event_dateStart` , `event_dateEnd` , `event_timeStart` , `event_timeEnd` , `event_location` , `event_geocode` , `event_url` , `event_tel` , `event_email` , `event_approved` , `event_uniqID` )
VALUES (
'$eventid', '', '$eventname', 'cat000', '$categorydescription', '$mincurrentprice', '".$datesplit[0]."', '".$datesplit[0]."', '".$datesplit[1]."', '', '$venuecity', '', '$eventurl', '0000000000', '', '1', '".mt_rand(10000, 10000000000)."'
)";

		 mysql_select_db($database_system_conn, $system_conn);
		$Result1 = mysql_query($insertSQL, $system_conn) or die(mysql_error());

		echo "$eventname added <br/><br/>";


		}else{
		echo "$eventname already exists<br/><br/>";
		}


  }
  }
}

 

Thanks for your help

 

Lee

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.