echo simplexml object

 ̄綄美尐妖づ 提交于 2020-05-23 10:52:09

问题


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 = "";
}
?>

回答1:


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

  • http://php.net/manual/fr/simplexmlelement.asxml.php

  • http://spotlesswebdesign.com/blog.php?id=14




回答2:


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.




回答3:


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.




回答4:


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.



来源:https://stackoverflow.com/questions/11006390/echo-simplexml-object

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!