Jump to content

XPath and searching in a XML document


andyfarang

Recommended Posts

Hello,

 

I want to search for a PRICE of product by his ID.

 

I have this unussual XML structure. I found some examples for XPath. But problem is, that in my XML id is not "parent" and price is not a "child". They are equal. Could somebody help me and show me, how to get a price if I know id of that product?

<eshop>
   <product>
      <id></id>
      <name></name>
      <price></price>
   </product>
   <product>
      <id></id>
      <name></name>
      <price></price>
   </product>
      <product>
      <id></id>
      <name></name>
      <price></price>
   </product>
</eshop>
Link to comment
Share on other sites

You just need to add another stage to the process.  Pull out the products with the xpath query and then create an array of product objects, then iterate through that array to find the product with the id that you want and retrieve the info you need:

$xmlObj = new SimpleXMLElement($xmlData);
$productsArray = $xmlObj->xpath('/eshop/product');
//...
//do some stuff
//...
//then when you need it:
foreach($productsArray as $idx=>$productObj){
  if($productObj->id == $knownIdYourLookingFor){
  $currentProductPrice = $productObj->price;
}

That gets you what you're looking for - however, it's not the best solution for a busy cart system, what would be better would be to re-index the $products array so that the keys reflect the product id. then you can quickly refferance it multiple times without having to repeatedly loop through it.

//top part stays the same
foreach($productsArray as $idx=>$productObj){
  $products[$productObj->id] = $productObj;
}
unset($productsArray); // don't really nead this, but as product lists can be sizable there's no point having multiple coppies about the place
//now you can grab what you need, when you need it directly from the $products array
$priceYouWant = $products[$idYouKnow]->price;

Let us know how that works for you.

 

::Edit - missed out a vary important "not" - fixed now

Link to comment
Share on other sites

Here's an example

$datax = "<eshop>
   <product>
      <id>35</id>
      <name>aaaaa</name>
      <price>50.00</price>
   </product>
   <product>
      <id>20</id>
      <name>bbbb</name>
      <price>100.00</price>
   </product>
      <product>
      <id>44</id>
      <name>ccccc</name>
      <price>70.00</price>
   </product>
</eshop>";

$x = simplexml_load_string($datax);

$prods = $x->xpath("//product[id=20]");  // find products with id = 20

echo $prods[0]->price;    //--> 100.00
Link to comment
Share on other sites

Thank you for both answers!

 

Additional question - XML has about 1000 products (524kB) and I have to update into MySql prices of about 200 products. Which of the solutions would be better to use? It seems that in Muddy_Funsters solution I would go through that XML at least ten thousand times?

Link to comment
Share on other sites

For that scenario you would be better using the method @barand has described, that way you only need to loop through the 200 id's that you want to update, rather than having to loop through the 1000 products and then the 200 id's.

 

assuming your 200 id's are in an array:

//load in your xml element as before - going to call it $xml in this
//also going to add a bit to build some of the query string to save another loop
$setStr = "";
foreach($idArray as $key=>$id){
  $prod = $xml->xpath("/eshop/product[id={$id}]");
  $update[$id] = $prod->price;
  $setStr .= "\tWHEN {$id} THEN {$prod->price}\n"
}

that will give you an array of the prices that you want to update in the database

 

::Edit - changed $setStr to set both values directly as source id data feed rather than user input.

Link to comment
Share on other sites

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.