Normally the XML files I have to parse are like this:
<row id="1">
<title>widget<title>
<color>blue<color>
<price>five<price>
</row>
Which I would then parse like this:
$xmlstr_widget = file_get_contents($my_xml_feed);
$feed_widget = new SimpleXMLElement($xmlstr_widget);
foreach($feed_widget as $name) {
$title = $name->title;
$color = $name->color;
$price = $price->price;
}
Works great! But now I have xml in a bit different format and I am a little stumped since I don’t have a whole lot of xml parsing experience:
<Widget Title="large" Color="blue" Price="five"/>
<Widget Title="small" Color="red" Price="ten"/>
How do I drill into that a little further and get it parsed properly? I have tried a few things but no success.
So the problem is when I try something like below with the different xml feed, I cannot echo any results.
foreach($feed_widget as $name) {
$title = $name->title;
$color = $name->color;
$price = $price->price;
}
You need to use the attributes() of the element.
For example, you’d want to do
would give you “blue”
Resource: http://www.w3schools.com/xml/xml_attributes.asp