Parsing XML using XPath and querystring variable

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
bynary
Forum Newbie
Posts: 4
Joined: Mon Feb 09, 2009 6:15 pm

Parsing XML using XPath and querystring variable

Post by bynary »

I am trying to use an ID retrieved from the querystring to get a node from an xml file. I then need to extract each child node from that node into a variable. Here's my code:

Code: Select all

 
<?php 
    $carid=$_GET['id']; 
            
    $xml = simplexml_load_file("xml/inventory.xml");    
    $car = $xml->xpath("/dataroot/XMLUpdateQuery[IDsuffix='$carid']");
    $year = $car->xpath("YEAR");
    $model = $car->xpath("MODEL");
    $make = $car->xpath("MAKE");
    $price = $car->xpath("ASKINGPRICE");
    $color = $car->xpath("COLOR");
    $comments = $car->xpath("COMMENTS");
    $milesbeginning = $car->xpath("MILESBEGINNING");
?>
 
I know this isn't right, this just illustrates what I'm trying to do. Any help would be appreciated.
t2birkey
Forum Commoner
Posts: 28
Joined: Mon Feb 09, 2009 8:41 pm

Re: Parsing XML using XPath and querystring variable

Post by t2birkey »

You were on the right track, but you went too far. If you want to get all the details from the car by its id you just need to say. $car will now be an array that holds: year, model, make, price, color, comments, and milesbeginning.

Your xml file will look similar to this:

Code: Select all

 
<dataroot>
    <car1>
        <YEAR>2009</YEAR>
        <MODEL>BMW</MODEL>
        <MAKE>325i</MAKE>
    </car1>
</dataroot>
 

Code: Select all

 
<?php
    $carid=(is_numeric($_GET['id']))?$_GET['id']:1; //set the id to 1 if they enter a word or something crazy
    $xml = simplexml_load_file("xml/inventory.xml");
    $result = $xml->xpath("dataroot/car".$id); //you could also do $xml->xpath("car".$id); 
    print_r($result);
?>
 
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: Parsing XML using XPath and querystring variable

Post by Weirdan »

bynary, and what isn't working with the code you're posted? Any error messages you're getting? What's your xml structure (post an example)?
bynary
Forum Newbie
Posts: 4
Joined: Mon Feb 09, 2009 6:15 pm

Re: Parsing XML using XPath and querystring variable

Post by bynary »

Sure...here's a sample of my xml:

Code: Select all

 
<?xml version="1.0" encoding="UTF-8"?>
<dataroot xmlns:od="urn:schemas-microsoft-com:officedata" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  xsi:noNamespaceSchemaLocation="inventory.xsd" generated="2009-01-31T11:57:25">
  <XMLupdateQry>
    <STATUS>ACTIVE</STATUS>
    <IDsuffix>465</IDsuffix>
    <VIN>1G8AJ55F06Z139166</VIN>
    <YEAR>2006</YEAR>
    <MAKE>Saturn</MAKE>
    <MODEL>Ion 2</MODEL>
    <MILESBEGINNING>41853</MILESBEGINNING>
    <COLOR>red</COLOR>
    <OPTIONS>This car runs and drives great with a 2.2L 4 cylinder engine and an automatic transmission. It has very low miles and features CD, power locks, and AC. It is a clean little car that is ready to go.</OPTIONS>
    <COMMENTS> </COMMENTS>
    <ASKINGPRICE>6000</ASKINGPRICE>
    <PROBLEMS> </PROBLEMS>
    <KBBRetail>10020</KBBRetail>
    <KBBPPE>8070</KBBPPE>
    <KBBPPG>7495</KBBPPG>
    <KBBPPF>6770</KBBPPF>
  </XMLupdateQry>
  <XMLupdateQry>
    <STATUS>ACTIVE</STATUS>
    <IDsuffix>464</IDsuffix>
    <VIN>1MEFM50U03A603185</VIN>
    <YEAR>2003</YEAR>
    <MAKE>Mercury</MAKE>
    <MODEL>Sable GS</MODEL>
    <MILESBEGINNING>84166</MILESBEGINNING>
    <COLOR>silver</COLOR>
    <OPTIONS>For sale is this 2003 Mercury Sable. This vehicle is in overall condition with a 3.0L V6 and an automatic transmission. The car features AC, cruise control, remote entry, and all power options.</OPTIONS>
    <COMMENTS> The car currently has a check engine light on which we hope to diagnose and repair.</COMMENTS>
    <ASKINGPRICE>2800</ASKINGPRICE>
    <PROBLEMS>The front bumper does have some damage. See picture.</PROBLEMS>
    <KBBRetail>5575</KBBRetail>
    <KBBPPE>3900</KBBPPE>
    <KBBPPG>3475</KBBPPG>
    <KBBPPF>3000</KBBPPF>
  </XMLupdateQry>
</dataroot>
 
...and here's the error message I'm getting:

Fatal error: Call to a member function xpath() on a non-object in /print.php on line 6
bynary
Forum Newbie
Posts: 4
Joined: Mon Feb 09, 2009 6:15 pm

Re: Parsing XML using XPath and querystring variable

Post by bynary »

Also, the variables $make, $model, and etc. are not getting populated. I'm not even sure if the $car array is getting populated.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Parsing XML using XPath and querystring variable

Post by John Cartwright »

Instead of checking your $car variable (which doesn't exist in the last posted snipplet), check the contents of $result.

Otherwise change $result to $car
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: Parsing XML using XPath and querystring variable

Post by Weirdan »

you query is invalid (for the xml structure you have posted), it should look something like this:

Code: Select all

 
$car = $xml->xpath("/dataroot/XMLUpdateQuery[./IDsuffix/text()='$carid']");
// or 
$car = $xml->xpath("/dataroot/XMLUpdateQuery/IDsuffix[text()='$carid']/..");
 
meaning XMLUpdateQuery nodes that have IDSuffix children with text content equal to $carid
bynary
Forum Newbie
Posts: 4
Joined: Mon Feb 09, 2009 6:15 pm

Re: Parsing XML using XPath and querystring variable

Post by bynary »

Maybe I'm not being clear enough with my request. Let me try some pseudocode...

Code: Select all

 
Get car id from querystring
 
Open xml document using simplexml_load_file and assign it to variable "xml"
Get the <car> child node of the xml document where the <ID> tag's innertext is equal to the car id
 
Get the <YEAR> node of the child node and assign its innertext to variable "year"
Get the <MAKE> node of the child node and assign its innertext to variable "make"
Get the <MODEL> node of the child node and assign its innertext to variable "model"
 
My xml has the following structure:

Code: Select all

 
<root>
  <car>
    <ID>1</ID>
    <year>2002</year>
    <make>Honda</make>
    <model>Civic</model>
  </car>
  <car>
    <ID>2</ID>
    <year>2008</year>
    <make>Volvo</make>
    <model>XC90</model>
  </car>
</root>
 
I appreciate what's been posted so far, but it has not been very helpful. I can get the ID from the querystring. I can load the xml document just fine, but I can't seem to get the xpath correct. I always get an empty array.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: Parsing XML using XPath and querystring variable

Post by Weirdan »

Code: Select all

<?php
$carId = 1;
$xmlContents = <<<EOF
<root>
  <car>
    <ID>1</ID>
    <year>2002</year>
    <make>Honda</make>
    <model>Civic</model>
  </car>
  <car>
    <ID>2</ID>
    <year>2008</year>
    <make>Volvo</make>
    <model>XC90</model>
  </car>
</root>
EOF;
 
$xml = simplexml_load_string($xmlContents);
 
list($car) = $xml->xpath('/root/car[ID="' . $carId . '"]');
$year = $car->year;
$make = $car->make;
$model = $car->model;
 
echo "Car:";
var_dump($car);
echo <<<EOF
Car properties:
        Year: $year
        Made by: $make
        Model: $model

EOF;

Code: Select all

Car:object(SimpleXMLElement)#2 (4) {
  ["ID"]=>
  string(1) "1"
  ["year"]=>
  string(4) "2002"
  ["make"]=>
  string(5) "Honda"
  ["model"]=>
  string(5) "Civic"
}
Car properties:
        Year: 2002
        Made by: Honda
        Model: Civic
Post Reply