Michael
Michael

Reputation: 337

Parsing XML with PHP

I'm using this PHP to parse a XML file:

<?php
$xml = simplexml_load_file("http://api.imgur.com/2/image/zzFV5.xml");

echo $xml->getName() . "<br />";

foreach($xml->children() as $child) {
    echo $child->getName() . ": " . $child . "<br />";
}
?>

How can I display the parsed content from the XML in my HTML file? Something like:

Image Views:<br/>
<?php echo ( $_REQUEST['views']) . ;?>

Thanks for your tips.

Upvotes: 1

Views: 369

Answers (3)

user2408661
user2408661

Reputation: 1

<html>
<body>
   <?php
    $html="";
    $url="http://www.espncricinfo.com/rss/content/story/feeds/586733.rss";
    $xml=simplexml_load_file($url);
    for($i=0;$i<10;$i++)
    {
        $title=$xml->channel->item[$i]->title;
        $html.="<div><center>$i.$title</center></div><hr/>";
    }
    echo $html ;
   ?>
</body>
</html>

Upvotes: 0

gandil
gandil

Reputation: 5418

You should state problem with more details. You can use asXML(), SimpleXMLElement::asXML Which will return a well-formed XML string based on SimpleXML element.

examplenode : The node which contains image

$xml_str = file_get_contents("file.xml");
$xml = simplexml_load_string($xml_str);
$result = $xml->xpath('//examplenode');
echo $result[0]->asXML();

Upvotes: 0

georgiecasey
georgiecasey

Reputation: 23391

$xml = simplexml_load_file("http://api.imgur.com/2/image/zzFV5.xml");
echo "Views: " . $xml->image->views;

That code outputs the views of that image from the XML file. Is that what you want?

Upvotes: 1

Related Questions