3

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 = "";
}
?>
hakre
  • 193,403
  • 52
  • 435
  • 836

4 Answers4

12

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

RafaSashi
  • 16,483
  • 8
  • 84
  • 94
2

Part 1

This is still not exactly what I wanted, but rather a very tidy and organized way of echoing the xml data:

    $url = "http://www.blogger.com/feeds/6552111825067891333/posts/default";
$xml = simplexml_load_file($url);
echo '<pre>';
print_r($xml);

Part 2

I had to get firephp running so I could see exactly what elements php was encountering when it reached an entry without a blog title. Ultimately it is an empty array. Therefore, the simple:

if(empty($entry->title))

works perfectly. For string comparison, I found that you can simply cast it as a string. For my purposes, that was unnecessary.

1

The simplexml_load_file returns an SimpleXMLElement, so:

print_r($xml);

will show its minor objects and arrays.

After your tweaks you can call $xml->asXML("filename.xml"); as @Tim Withers pointed out.

0

Part 1: echo $xml->asXML(); - http://www.php.net/manual/en/simplexmlelement.asxml.php

Part 2: php SimpleXML check if a child exists

$html .= "<h3 class='blog'>".($entry->title!=null?$entry->title:'No Title')
    . "<span class='pubDate'> | " .$pubDate . "</span></h3><p class='blog'>" 
    . $entry->content . "</p>";

Note I would probably load the url like this:

$feedUrl = 'http://www.blogger.com/feeds/6552111825067891333/posts/default';
$rawFeed = file_get_contents($feedUrl);
$xml = new SimpleXmlElement($rawFeed);

Based on your comment in regards to part 1, I am not sure if the XML is being loaded completely. If you try loading it this way, it should display all the XML data.

Community
  • 1
  • 1
Tim Withers
  • 12,072
  • 5
  • 43
  • 67
  • Part 1: I tried the asXML command, and it is still giving me abridged version of the xml. Part 2: According to the answer, I should check if it is null. I am inclined to then infer that I could fill that potentially null element with a string. Would you agree with that? If so, could you give me a hint as to which function to use? –  Jun 13 '12 at 00:00
  • Thanks Tim. Now how about printing the unabridged xml on the page? Checkout the difference; here is the dummy blog page: http://www.cobracabanarecords.com/index2.php. The RSS feed is here: http://www.blogger.com/feeds/6552111825067891333/posts/default –  Jun 13 '12 at 00:13
  • Actually, your last edit didn't resolve the issue as I thought it would. I added this:$feedUrl = "http://www.blogger.com/feeds/6552111825067891333/posts/default";$rawFeed = file_get_contents($feedUrl);$xml = newSimpleXMLElement($rawFeed); I then tried print_f, echo, and echo $xml->asXML(various variables tried). I am still getting the short version of the XML. –  Jun 13 '12 at 00:22
  • Also the syntax you supplied for Part 2 did not work. Thanks for your time. –  Jun 13 '12 at 00:29