php - 回显 simplexml 对象

标签 php html rss simplexml blogger

这个问题有两个部分。

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

我相信语法是这样的:

$xml = simplexml_load_file($url);
echo $xml;

我又试了一次,但没有用,所以显然我忘记了正确的语法,亲爱的 stackoverflow 问答者,我需要你的帮助。

我一直试图弄清楚我在做什么,但我无法在 Google 或 PHP 网站上找到示例。我试过 print_r($url);命令,它给了我似乎是提要的原子化版本。我想要整根绳子,包括疣和所有的东西。我意识到我可以直接在窗口中输入 RSS 链接并查看它,但是在我编码和点头时将它放在我的 PHP 页面上很有帮助。

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

那么遇到这种情况该怎么办呢?我试了一下:

if(entry->title == "")
{$entryTitle = "untitled";}

那根本行不通。

这是我处理博客的整个 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 = "";
}
?>

最佳答案

根据 php 手册:

$xml = new SimpleXMLElement($string);
.
.
.

那么如果你想回显结果:

echo $xml->asXML();

或将 xml 保存到文件中:

$xml->asXML('blog.xml'); 

引用资料

关于php - 回显 simplexml 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11006390/

相关文章:

iPhone SDK - 从 RSS feed 解析 XML 时出现问题

php - 使用包含 HTML 的行创建 RSS Feed 会导致 Feed 为空

java - 用 Java 写一个 RSS 阅读器

php - 如何从从 mysqli_fetch_array() 获取的数组转换的 javascript 数组中获取所有行数据

html - CSS 网页宽度、导航栏居中和小部件环绕居中

php - Docker 容器上的 Couchbase PHP SDK 2.6

javascript - jquery 删除动态添加的元素

html - 选择列表中的 li

php - Laravel二维码: endroid/QrCode,如何使用呢?

php - CodeIgniter:允许的内存耗尽