RJD22
RJD22

Reputation: 10340

Run XPath over an XPath result within a Foreach loop

I'm working on a project that reads datafeeds like these:

<?xml version="1.0" encoding="UTF-8"?>
<producten>
  <product>
    <sku><![CDATA[75100192B]]></sku>
    <price><![CDATA[349.00]]></price>
    <old_price><![CDATA[499.00]]></old_price>
    <in_stock><![CDATA[1]]></in_stock>
    <delivery_time><![CDATA[1 - 2 werkdagen]]></delivery_time>
    <manufacturer><![CDATA[Kids River]]></manufacturer>
    <name><![CDATA[Kids River Urban Stroller 3 in 1 Black / Grey  Black Frame]]></name>
    <description><![CDATA[KidsRiver Urban Stroller]]></description>
    <url><![CDATA[http://www.url.com/]]></url>
    <categorie><![CDATA[Kinderwagens]]></categorie>
    <subcategorie><![CDATA[Kinderwagen]]></subcategorie>
    <model><![CDATA[Kids River Urban]]></model>
    <collectiejaar><![CDATA[2011]]></collectiejaar>
    <shipping_price><![CDATA[0.00]]></shipping_price>
    <image><![CDATA[http://www.url.com/image.jpg]]></image>
  </product>
  <product>
    <sku><![CDATA[Joolz Day Silver / Black - 2012]]></sku>
    <price><![CDATA[849.00]]></price>
    <old_price><![CDATA[879.00]]></old_price>
    <in_stock><![CDATA[1]]></in_stock>
    <delivery_time><![CDATA[1 - 2 werkdagen]]></delivery_time>
    <manufacturer><![CDATA[Joolz]]></manufacturer>
    <name><![CDATA[Joolz Day Silver / Black]]></name>
    <description><![CDATA[Joolz Day Compleet.]]></description>
    <url><![CDATA[http://www.url.com/]]></url>
    <categorie><![CDATA[Kinderwagens]]></categorie>
    <subcategorie><![CDATA[Kinderwagen]]></subcategorie>
    <model><![CDATA[Joolz Day]]></model>
    <collectiejaar><![CDATA[2012]]></collectiejaar>
    <shipping_price><![CDATA[0.00]]></shipping_price>
    <image><![CDATA[http://www.url.com/image.jpg]]></image>
  </product>
</producten>

What I want to do it to make a loop that can read individual products from this feed so I made something like this:

// Spin up the SimpleXML parser
$xml_feed = new SimpleXMLElement($feed_data);

$products = $xml_feed->xpath('//producten/product');

foreach ($products as $product)
{
    $name = $product->xpath('./name');
    $price = $product->xpath('price');
    $old_price = $product->xpath('old_price');
    $in_stock = $product->xpath('in_stock');
    $manufacturer = $product->xpath('manufacturer');

    echo print_r( $name);
    echo var_dump((string) $price[0]);
    echo var_dump((string) $old_price[0]);
    echo var_dump((string) $in_stock[0]);
    echo var_dump((string) $manufacturer[0]);
    echo "\r\n";
}

I was wondering if there is a better way to read a xml when using XPath 2 times. One before the loop and the other XPath within the loop. I'm doing it this way because I want to be able to parse all kinds of feeds.

I'm a little concerned about the [0] behind the field: $price[0].

Upvotes: 0

Views: 1302

Answers (1)

Tomalak
Tomalak

Reputation: 338208

"I was wondering if there is a better way to read a xml when using XPath 2 times. One before the loop and the other XPath within the loop."

Why? I can see nothing wrong with that.

"I'm a little concerned about the [0] behind the field: $price[0]."

xpath() always returns a node list, even if there is only one node to select. The [0] is correct.


Anyway, since you're using SimpleXML, you could write:

$producten = new SimpleXMLElement($feed_data);

foreach ($producten->product as $product) 
{
  echo $product->price . "\n";
  echo $product->old_price . "\n";
  echo $product->in_stock . "\n";
  echo $product->manufacturer . "\n";
}

http://codepad.org/SDDPBFhn

Upvotes: 2

Related Questions