echo simplexml object
According to the php
manual:
$xml = new SimpleXMLElement($string);
.
.
.
then if you want to echo
the result:
echo $xml->asXML();
or save the xml to a file:
$xml->asXML('blog.xml');
References
![Admin](/assets/logo_square_200-5d0d61d6853298bd2a4fe063103715b4daf2819fc21225efa21dfb93e61952ea.png)
Admin
Updated on July 12, 2022Comments
-
Admin almost 2 years
This question has two parts.
Part 1. Yesterday I had some code which would echo the entire content of the XML from an RSS feed. Then I deleted it from my php document, saved over it, and I am totally kicking myself.
I believe the syntax went something like this:
$xml = simplexml_load_file($url); echo $xml;
I tried that again and it is not working, so apparently I forgot the correct syntax and could use your help, dear stackoverflow question answerers.
I keep trying to figure out what I was doing and I am unable to find an example on Google or the PHP site. I tried the print_r($url); command, and it gives me what appears to be an atomized version of the feed. I want the whole string, warts and all. I realize that I could just type the RSS link into the window and see it, but it was helpful to have it on my PHP page as I am coding and noding.
Part 2 The main reason I wanted to reconstruct this is because I am trying to parse nodes off a blog RSS in order to display it on a webpage hosted on a private domain. I posted a dummy blog and discovered an awkward formatting glitch when I failed to add a title to one of the dummy posts.
So what does one do in this situation? I tried a little:
if(entry->title == "") {$entryTitle = "untitled";}
That did not work at all.
Here's my entire php script for the handling of the blog:
<?php /*create variables*/ $subtitle =""; $entryTitle=""; $html = ""; $pubDate =""; /*Store RSS feed address in new variable*/ $url = "http://www.blogger.com/feeds/6552111825067891333/posts/default"; /*Retrieve BLOG XML and store it in PHP object*/ $xml = simplexml_load_file($url); print_r($xml); /*Parse blog subtitle into HTML and echo it on the page*/ $subtitle .= "<h2 class='blog'>" . $xml->subtitle . "</h2><br />"; echo $subtitle; /*Go through all the entries and parse them into HTML*/ foreach($xml->entry as $entry){ /*retrieve publication date*/ $xmlDate = $entry->published; /*Convert XML timestamp into PHP timestamp*/ $phpDate = new DateTime(substr($xmlDate,0,19)); /*Format PHP timestamp to something humans understand*/ $pubDate .= $phpDate->format('l\, F j\, Y h:i A'); if ($entry->title == "") { $entryTitle .= "Untitled"; } echo $entry->title; /*Pick through each entry and parse each XML tree node into an HTML ready blog post*/ $html .= "<h3 class='blog'>".$entry->title . "<span class='pubDate'> | " .$pubDate . "</span></h3><p class='blog'>" . $entry->content . "</p>"; /*Print the HTML to the web page*/ echo $html; /*Set the variables back to empty strings so they do not repeat data upon reiteration*/ $html = ""; $pubDate = ""; } ?>