1

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.

1
  • There's not really enough information here to help you. If you want to just display everything from the XML you can just change your echo statements to variable assignments. ex: $html .= $xml->getName() . "<br />";, but if you're looking to display a specific part of your XML, you'll need to be more explicit Commented Apr 20, 2011 at 0:26

3 Answers 3

1
$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?

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

2 Comments

Yes that's it! :) Sorry I'm new to PHP and XML. What it's the cleanest way to write this? What can I optimize?: <?php $xml = simplexml_load_file("http://api.imgur.com/2/image/zzFV5.xml"); echo $xml->image->views; echo $xml->image->datetime; echo $xml->image->type; ?> <u>Views:</u><br/> <?php ( $_REQUEST['views']) ;?> <br/> <u>Date:</u><br/> <?php ( $_REQUEST['datetime']) ;?> <br/> <u>Type:</u><br/> <?php ( $_REQUEST['type']) ;?>
@Michael I don't know what you're trying to do with the $REQUEST variable, I presume you meant $_REQUEST and if so, it's got nothing to do what you're trying to do. I'm guessing this is the code you want: <?php $xml = simplexml_load_file("http://api.imgur.com/2/image/zzFV5.xml"); ?> <u>Views:</u><br/> <?php echo $xml->image->views; ?> <br/> <u>Date:</u><br/> <?php echo $xml->image->datetime; ?> <br/> <u>Type:</u><br/> <?php echo $xml->image->type; ?>
0

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();

1 Comment

@georgiecasey Awesome! Now I understand how it works. Thanks for lighting me up!
0
<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>

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.