echo simplexml对象

1 html php rss blogger simplexml

这个问题有两个部分.

第1部分.昨天我有一些代码可以从RSS提要中回显XML的全部内容.然后我从我的php文档中删除它,保存在它上面,我完全在踢自己.

我相信语法是这样的:

$xml = simplexml_load_file($url);
echo $xml;
Run Code Online (Sandbox Code Playgroud)

我再次尝试了它并且它不起作用,所以显然我忘记了正确的语法并且可以使用你的帮助,亲爱的stackoverflow问题回答者.

我一直想弄清楚我在做什么,我无法在Google或PHP网站上找到一个例子.我试过print_r($ url); 命令,它给了我一个似乎是源的雾化版本.我想要整个弦乐,疣和所有.我意识到我可以在窗口中输入RSS链接并查看它,但是当我编码和编码时,将它放在我的PHP页面上会很有帮助.

第2部分我想重建这个的主要原因是因为我试图从博客RSS解析节点,以便在私有域上托管的网页上显示它.我发布了一个虚拟博客,当我没有为其中一个虚拟帖子添加标题时发现了一个尴尬的格式化故障.

那么在这种情况下做什么呢?我试了一下:

if(entry->title == "")
{$entryTitle = "untitled";}
Run Code Online (Sandbox Code Playgroud)

那根本不起作用.

这是我处理博客的完整PHP脚本:

<?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 = "";
}
?>
Run Code Online (Sandbox Code Playgroud)

Raf*_*shi 9

根据php手册:

$xml = new SimpleXMLElement($string);
.
.
.
Run Code Online (Sandbox Code Playgroud)

那么如果你想得到echo结果:

echo $xml->asXML();
Run Code Online (Sandbox Code Playgroud)

或者将xml保存到文件中:

$xml->asXML('blog.xml'); 
Run Code Online (Sandbox Code Playgroud)

参考