0

Pls see snippet of XML file below:

<products>
<product>
<id>2589527</id>
<name>Samsung PS42C450</name>
<manufacturer>Samsung</manufacturer>
<manufacturer-id>22</manufacturer-id>
<description>42 in, Widescreen, Plasma, HD Ready, Samsung DNIe+ ,</description>
<category>Sound and Vision &gt; Vision &gt; TVs</category>
<category-id>2</category-id>
<number-of-retailers>16</number-of-retailers>
<image-url height="217" width="300">http://images.pricerunner.com/product/300x217/101294172/Samsung-PS42C450.jpg</image-url>
<rating type="average">
  <average>4,1</average>
</rating>
<rating type="professional">
  <average>4,1</average>
  <num-ratings>1</num-ratings>
</rating>
<lowest-price currency="GBP">354.44</lowest-price>
<highest-price currency="GBP">549.00</highest-price>
</product>
</products>

I'm having problems parsing image-url, lowest-price and highest-price

I'm trying:

$lowprice = $products->lowest-price;

$highprice = $products->highest-price;

$imageURL = $products->image-url;

But they are returning nothing - any ideas what I'm doing wrong?

1
  • $url = 'http://*********/feedoutput.xml'; $filecontent = file_get_contents($url); $xml = simplexml_load_file($url) or die("feed not loading"); foreach ($xml->product as $products) { etc. etc. Commented Sep 30, 2011 at 19:24

4 Answers 4

1

use $products->{'lowest-price'}. (with curly braces you can use special characters such as the minus sign)

Shai.

Sign up to request clarification or add additional context in comments.

Comments

1
$products->lowest-price;

PHP will interpret as

($products->lowest)  minus price;

There's no "lowest" in your $products object, and price is almost certainly not a defined constant, so both show up as null, get cast to 0's, and end up producing 0-0=0

Comments

0

It's probably because of the dash (assuming other properties work). Try encapsulating them with curly braces, like

$products->{highest-price};

2 Comments

Aurimas, your example is looking for a key which is the result of highest minus price (an expression that doesn't exist) . When searching for a "stringed" key , you should use ' sign (like in my answer above' $products->{'highest-price'}
absolutely true, Shai, I missed it!
0

There are (maybe) two unrelated issues.

1. XML structure

Your (broken) code, e.g. $products->lowest-price is wanting to access the lowest-price element which is a child of $products. Assuming the variable is named after the element, you have a little more work to do. The XML is structured like

<products>
 └─ <product>
     └─ <lowest-price>

So if $products is the `products> element, then three steps are needed.

$products->product->lowest-price

The above may be a non-issue, and simply unfortunate variable naming.

2. Dash (-) in element name

When presented with $a->b-c, PHP sees that as $a->b <minus> c as b-c is not a valid label (and no a valid object property name). As noted on the "SimpleXML Basic usage" manual page (link), the appropriate solution is to use the variable property syntax ala.

$products->product->{'lowest-price'}

See: http://php.net/simplexml.examples-basic (Example #3)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.