PDA

View Full Version : XML Feed with value?



Dalton
May 2nd, 2006, 05:09 PM
Ok, Ive just started to learn how to use xml feeds, I picked up most of it in about a day, one thing I do not understand is how to access a certain tag with a value.
Im using



$xml = simplexml_load_file("news.xml");
foreach ($xml->news as $news) {
// echo content here
}

(pretty simple)

and I have an xml file


<post>
<news id="15">
<author>Dalton</author>
<story>blah</story>
</news>
<news id="14">
<author>Dalton</author>
<story>blah</story>
</news>
</post>


Now, on to my point, I want to echo only the news where the id="14" so it will only display where <news id="14"></news>.

I have asked so many people on how to do this and no one can seem to explain it to me, haha. PLEASE HELP.

Thanks,
Dalton Conley

Dalton
May 2nd, 2006, 08:20 PM
Ok, the problem has a simple solution that has been right in front of me the whole time!



<?php
$string = <<<XML
<a>
<foo name="one" game="lonely">1</foo>
</a>
XML;

$xml = simplexml_load_string($string);
foreach($xml->foo[0]->attributes() as $a => $b) {
echo $a,'="',$b,"\"\n";
}
?>


The $a variable is the attribute and the $b variable is the attributes value. You can also access attributes with the use of $xml['attribute_name'] which works very well too.

Thanks to all of those couragous people who posted - NOT! haha