parse / save XML data using Php / MySQL

I use curl using php and the successful URL call returns an XML (sample below).

 
<?xml version="1.0" encoding="UTF-8" ?>
<results>
	<product category="baby">
		<language>english</language>

		<value>160</value>

		<wholesale_value>10</wholesale_value>

		<stats>

			<sales year="2011">
				<type>broad</type>
				<avg_competition>low</avg_competition>
			</sales>

			<sales year="2010">
				<type>naarrow</type>
				<avg_competition>high</avg_competition>
			</sales>
		</stats>

	</product>
</results>

Open in new window


I need to parse the returned XML and save the information in a MySQL table. The primary identifier is       <product category="baby">, which is unique.

Any help is highly appreciated with a working PHP/ MySQL example to parse the XML and save it in the DB.
nainilAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

hieloCommented:
try using simplexml. The following link shows you how to use it:
http://www.willfitch.com/simplexml-tutorial-part1.html

the documentation/api is at:
http://php.net/manual/en/book.simplexml.php

Since you already have the remote xml as a string (the result from curl), you can use
simplexml_load_string() to create an object from it (this will make more sense once you read the first link)
0
dnzone88Commented:
here is a working example
<?php
$xml_str = '
<?xml version="1.0" encoding="UTF-8" ?>
<results>
	<product category="baby">
		<language>english</language>
		<value>160</value>
		<wholesale_value>10</wholesale_value>
		<stats>
			<sales year="2011">
				<type>broad</type>
				<avg_competition>low</avg_competition>
			</sales>
			<sales year="2010">
				<type>naarrow</type>
				<avg_competition>high</avg_competition>
			</sales>
		</stats>
	</product>
</results>';

$xml_obj = simplexml_load_string(trim($xml_str));

// save info directly into the database
if(count($xml_obj->product) > 0){
	foreach($xml_obj->product as $product){
		$key = $product['category'];
		$language =  $product->language;
		$value = $product->value;
		$wholesale = $product->wholesale_value;
		// create your query here
		$query = "insert into item (category, language, value, wholesale) values('$key', '$language', '$value', '$wholesale')";
		// execute $query here
		echo $query, '<br />';
		if(isset($product->stats)){
			$sales = $product->stats;
			if(count($sales->sales) > 0){
				foreach($sales->sales as $year){
					$yr = $year['year'];
					$type = $year->type;
					$avg = $year->avg_competition;
					// create your sub query here
					$query = "insert into sales (category, year, type, avg_competiton) values('$key', '$yr', '$type', '$avg')";
					// execute $query here
					echo $query, '<br />';
				}
			}
		}
	}
}
?>

Hope it help

Open in new window

0
Ray PaseurCommented:
http://www.laprbass.com/RAY_temp_nainil.php

Outputs: FOUND BABY
<?php // RAY_temp_nainil.php
error_reporting(E_ALL);
echo "<pre>";

$xml = <<<XML
<?xml version="1.0" encoding="UTF-8" ?>
<results>
	<product category="baby">
		<language>english</language>

		<value>160</value>

		<wholesale_value>10</wholesale_value>

		<stats>

			<sales year="2011">
				<type>broad</type>
				<avg_competition>low</avg_competition>
			</sales>

			<sales year="2010">
				<type>naarrow</type>
				<avg_competition>high</avg_competition>
			</sales>
		</stats>

	</product>
</results>
XML;

$obj = SimpleXML_Load_String($xml);

foreach ($obj->product as $p)
{
    if ($p["category"] == 'baby') echo "FOUND BABY";
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.