Problems extracing data from simple XML feed

I’m hoping someone might be able to help me extract the data that’s in the <name> and <colour> nodes in the XML field below:


<cars type="name">
   <build>
     <name>Data</name>
     <colour>Data</colour>
   </build>
   <build>
     <name>Data</name>
     <colour>Data</colour>
   </build>
</cars>

I’ve been using the following PHP code:


//find parent node for each product
case 'cars';

//initialise xml parser
$dom = new DOMDocument();
$domNode = $xmlReader->expand();
$element = $dom->appendChild($domNode);
$domString = utf8_encode($dom->saveXML($element));
$product = new SimpleXMLElement($domString);

//import data
$game = $cars->build->name;
$amount = $cars->build->colour;


//insert query into database

        if ($query = mysql_query("REPLACE INTO info
        (type, name, colour)
        
        VALUES ('Car', '$name', '$colour')"))
        
        {
        
        }
        
        else
        {
        echo "Problem with <strong>".$name."</strong>: ". mysql_error()."<br />";
        }
        
}
} 

Unfortunately though, when it comes to insert the values into the database, my name and colour variables seem to be empty.

Am I extracting the data incorrectly?

Thanks a lot for your help

Can anyone help at all?

Thank you very much in advance …

I looked at your example code and I’m sorry but it seems like unrelated pieces. i.e.

What is “case”, part of a switch?
Where and how are you passing the XML to the DOM functions?
Where is the $cars that’s being assigned to $game and $amount?
Where are the $name and $colour variables used in the query being assigned?

Ok, here’s the full PHP script:


<?
require_once('db_connect.inc');
db_connect();

$xmlReader = new XMLReader();

//define filename and location of feed
$filename = "car_data";
$url= "http://www.mydomain.com/xml.php";

//download content of XML feed
file_put_contents($filename, file_get_contents($url));

//read in xml file
$xmlReader->open($filename);

while ($xmlReader->read())
{
switch ($xmlReader->name)
{
//find parent node for each product
case 'cars';

//initialise xml parser
$dom = new DOMDocument();
$domNode = $xmlReader->expand();
$element = $dom->appendChild($domNode);
$domString = utf8_encode($dom->saveXML($element));
$product = new SimpleXMLElement($domString);

//import data
$name = $cars->build->name;
$amount = $cars->build->colour;

//insert query into database

        if ($query = mysql_query("REPLACE INTO info
        (type, name, colour)

        VALUES ('Car', '$name', '$colour')"))

        {        

        }

        else

        {

        echo "Problem with <strong>".$name."</strong>: ". mysql_error()."<br />";

        }

}
} 

?>



Well, I got it to get and echo those values OK but I made so many changes I don’t know if any of this will help you.

Instead of writing file output to a file and using ->open() I put the XML into a string and used ->XML() for testing purposes.

Then I replaced the switch case with:
if ( ($xmlReader->name == ‘build’) && ($xmlReader->nodeType == 1) )

Because the SimpleXMLElement is being assigned to $product, there is no $cars object. The root node doesn’t come into play, and since the reader is already inside of the build node (because of the if conditional) $product->name and $product->colour work fine, though it doesn’t feel right assigning ->colour to $amount especially when the query uses $colour

Thanks Mittineague - would you be able to post the final script that you came up with?

Thanks

Is there any particular reason why you’re mixing XMLReader and DOM/SimpleXML (is the XML particularly large, or are memory constraints very tight)?

Actually this feed isn’t particularly big - it’s just the way I’ve accessed slightly larger feeds in the past.

Would you recommend doing it another way?