回显 simplexml 对象


echo simplexml object

这个问题有两个部分。

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

我相信语法是这样的:

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

再次尝试了,但它不起作用,所以显然我忘记了正确的语法,可以使用您的帮助,亲爱的堆栈溢出问题答案。

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

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

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

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结果:

echo $xml->asXML();

或将 XML 保存到文件:

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

引用

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

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

1 部分

这仍然不是我想要的,而是一种非常整洁和有条理的回显xml数据的方式:

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

第 2 部分

必须让firephp运行,这样我才能准确地看到php在没有博客标题的条目时遇到了什么元素。最终它是一个空数组。因此,简单:

if(empty($entry->title))

完美工作。对于字符串比较,我发现您可以简单地将其转换为字符串。就我而言,这是不必要的。

simplexml_load_file返回一个 SimpleXMLElement,因此:

print_r($xml);

将显示其次要对象数组

调整后,您可以调用$xml->asXML("文件名.xml"); 正如@Tim Withers指出的那样。

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

第 2 部分:php SimpleXML 检查子项是否存在

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

请注意,我可能会像这样加载网址:

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

根据您对第 1 部分的评论,我不确定 XML 是否被完全加载。 如果尝试以这种方式加载它,它应该显示所有 XML 数据。