Page 1 of 1

Help me! Parsing XML file using php

Posted: Fri Jul 16, 2004 7:36 am
by ganeshkannan
feyd | Please use

Code: Select all

and

Code: Select all

tags where appropriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]


Hi All!,
         Pls can any one help to achieve parsing a yahoo store(xml) file using php. I have a code this code just takes all the data from yahoo store
but the problem is i cant able to get the images and some datas in xml file has "<" ">" tags so the script i have written is not functioning properly.

I am biginner to xml. So I need help from some one. 

And also i want to know how to get the name of the tag in xml .
(ie) if xml file has something like this..,

Code: Select all

&lt;ProductRef Id="gifts" Url="http://store.yahoo.com/acme/gifts.html"&gt;Gifts&lt;/ProductRef&gt;
i need to take the Id,Url of ProductRef tag? (Any Way?)

Pls Help me Out!

Thanx.,
Ganesh

Here is the code i have:

Code: Select all

<?php 

$xml_file = "http://store.yahoo.com/acme/objinfo.xml"; 

$xml_description_key = "^STOREEXPORT^PRODUCTS^PRODUCT^DESCRIPTION"; 
$xml_url_key = "^STOREEXPORT^PRODUCTS^PRODUCT^URL";
$xml_orderable_key = "^STOREEXPORT^PRODUCTS^PRODUCT^ORDERABLE";
$xml_taxable_key = "^STOREEXPORT^PRODUCTS^PRODUCT^TAXABLE";
$xml_code_key="^STOREEXPORT^PRODUCTS^PRODUCT^CODE";
$xml_thumb_key="^STOREEXPORT^PRODUCTS^PRODUCT^THUMB";
$xml_picture_key="^STOREEXPORT^PRODUCTS^PRODUCT^PICTURE";
$xml_weight_key="^STOREEXPORT^PRODUCTS^PRODUCT^WEIGHT";

$xml_availability_key="^STOREEXPORT^PRODUCTS^PRODUCT^AVAILABILITY";
$xml_caption_key="^STOREEXPORT^PRODUCTS^PRODUCT^CAPTION";

$xml_baseprice_key="^STOREEXPORT^PRODUCTS^PRODUCT^PRICING^BASEPRICE";
$xml_localizedbaseprice_key="^STOREEXPORT^PRODUCTS^PRODUCT^PRICING^LOCALIZEDBASEPRICE";
$xml_giftwrapcharge_key="^STOREEXPORT^PRODUCTS^PRODUCT^PRICING^GIFTWRAPCHARGE";

$xml_optionvalue_key="^STOREEXPORT^PRODUCTS^PRODUCT^OPTIONLISTS^OPTIONLIST^OPTIONVALUE";

$product_array = array(); 

$counter = 0; 
class xml_products{ 
	var $url; 
    	var $description;
    	var $orderable;
	var $taxable;
	var $code;
	var $thumb;
	var $picture;    
	var $weight;
    	var $baseprice;
	var $localizedbaseprice;
	var $giftwrapcharge;
	var $availability;
	var $caption;
	var $optionvalue = array();
} 

function startTag($parser, $data){ 
    global $current_tag; 
	$current_tag .= "^$data"; 
} 

function endTag($parser, $data){ 
    global $current_tag; 
	
	$tag_key = strrpos($current_tag, '^'); 
    $current_tag = substr($current_tag, 0, $tag_key); 
} 

function contents($parser, $data){ 
    global $current_tag, $xml_orderable_key, $xml_description_key,$xml_url_key, $counter, $product_array; 
    global $xml_taxable_key,$xml_code_key,$xml_thumb_key,$xml_picture_key,$xml_weight_key ;
    global $xml_baseprice_key,$xml_localizedbaseprice_key,$xml_giftwrapcharge_key;
    global $xml_availability_key,$xml_caption_key;
    global $xml_optionvalue_key;

 switch($current_tag){ 
	
	case $xml_url_key: 
            	$product_array[$counter] = new xml_products(); 
  	        $product_array[$counter]->url = $data; 
            	break;
	
	case $xml_orderable_key:
		$product_array[$counter]->orderable = $data; 
		break;

	case $xml_availability_key:
		$product_array[$counter]->availability = $data; 
		break;

	case $xml_caption_key:
		$product_array[$counter-1]->caption = $data; 
		break;
	
	case $xml_taxable_key:
		$product_array[$counter]->taxable = $data; 
		break;

	case $xml_code_key:
		$product_array[$counter]->code = $data; 
		break;

	case $xml_thumb_key:
			$data=htmlentities ($data);
		$product_array[$counter]->thumb = $data; 
		break;

	case $xml_picture_key:
		$data=htmlentities($data);
		$product_array[$counter]->picture = $data; 
		break;

	case $xml_weight_key:
		$product_array[$counter]->weight = $data; 
		break;

	case $xml_baseprice_key:
		$product_array[$counter-1]->baseprice = $data; 
		break;

	case $xml_localizedbaseprice_key:
		$product_array[$counter-1]->localizedbaseprice = $data; 
		break;

	case $xml_giftwrapcharge_key:
		$product_array[$counter-1]->giftwrapcharge = $data; 
		break;

	case $xml_optionvalue_key:
		$product_array[$counter-1]->optionvalue[] = $data; 
		break;


	case $xml_description_key: 
            	$product_array[$counter]->description = $data; 
            	$counter++; 
            	break; 
    } 
} 

$xml_parser = xml_parser_create(); 

xml_set_element_handler($xml_parser, "startTag", "endTag"); 

xml_set_character_data_handler($xml_parser, "contents"); 

$fp = fopen($xml_file, "r") or die("Could not open file"); 

//$data = fread($fp, filesize($xml_file)) or die("Could not read file"); 

$data=file($xml_file);
$data=join("",$data);



if(!(xml_parse($xml_parser, $data, feof($fp)))){ 
    die("Error on line " . xml_get_current_line_number($xml_parser)); 
} 

xml_parser_free($xml_parser); 

fclose($fp); 

//print_r($product_array);
//exit;

?> 


<html> 
<head> 
<title>Yahoo Store</title> 
</head> 
<body bgcolor="#FFFFFF"> 
<? 
// A simple for loop that outputs our final data. 
for($x=0;$x<count($product_array);$x++){ 
    echo "\t<h2>" . $product_array[$x]->product . "</h2>\n"; 
    echo "\t\t\n"; 
    echo "\t<b>Description</b> :<i>" . $product_array[$x]->description . "</i><br>"; 
    echo "\t<b>Url</b> :<i>" . $product_array[$x]->url . "</i><br>"; 
    echo "\t<b>Orderable</b> :<i>" . $product_array[$x]->orderable . "</i><br>"; 
    echo "\t<b>Taxable</b> :<i>" . $product_array[$x]->taxable . "</i><br>"; 
    echo "\t<b>Code</b> :<i>" . $product_array[$x]->code . "</i><br>"; 
    echo "\t<b>Thumb</b> :<i>" . $product_array[$x]->thumb . "</i><br>"; 
    echo "\t<b>Picture</b> :<i>" . $product_array[$x]->picture . "</i><br>"; 
    echo "\t<b>Weight</b> :<i>" . $product_array[$x]->weight . "</i><br>"; 
    echo "\t<b>Base Price</b> :<i>" . $product_array[$x]->baseprice . "</i><br>"; 
    echo "\t<b>LocalizedBasePrice</b> :<i>" . $product_array[$x]->localizedbaseprice . "</i><br>"; 
    echo "\t<b>Availability</b> :<i>" . $product_array[$x]->availability . "</i><br>"; 
    echo "\t<b>Caption</b> :<i>" . $product_array[$x]->caption . "</i><br>"; 
    echo "\t<b>Option Value Count</b> :<i>" . count($product_array[$x]->optionvalue) . "</i><br>"; 
    print_r($product_array[$x]->optionvalue);	
    echo "<br>";

    echo "<hr>";
} 
?> 

</body> 
</html>

feyd | Please use

Code: Select all

and

Code: Select all

tags where appropriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]

XML

Posted: Sun Jul 18, 2004 5:43 am
by stsr11
Have a look at this...

http://keithdevens.com/software/phpxml

It's a simple XML parser that reads XML into an array. :wink:

Just read your XML using 'file' as you are doing, 'implode' the result into a string and then pass it to '$myXML = XML_unserialize($string);'.

Use 'print_r' or 'var_dump' to show the structure of the resultant array.

Hope this helps... :D

Thank You!

Posted: Tue Jul 20, 2004 5:31 am
by ganeshkannan
Thank You! The link you send to me helped me a Lot!