Php echo simplexml对象
这个问题有两部分 第1部分。昨天我有一些代码,可以从RSS提要中回显XML的全部内容。然后我把它从我的php文档中删除,保存在上面,我完全是在踢自己 我相信语法是这样的:Php echo simplexml对象,php,html,rss,simplexml,blogger,Php,Html,Rss,Simplexml,Blogger,这个问题有两部分 第1部分。昨天我有一些代码,可以从RSS提要中回显XML的全部内容。然后我把它从我的php文档中删除,保存在上面,我完全是在踢自己 我相信语法是这样的: $xml = simplexml_load_file($url); echo $xml; $feedUrl = 'http://www.blogger.com/feeds/6552111825067891333/posts/default'; $rawFeed = file_get_contents($feedUrl); $
$xml = simplexml_load_file($url);
echo $xml;
$feedUrl = 'http://www.blogger.com/feeds/6552111825067891333/posts/default';
$rawFeed = file_get_contents($feedUrl);
$xml = new SimpleXmlElement($rawFeed);
亲爱的stackoverflow问题解答者,我又试了一次,但它不起作用,所以显然我忘记了正确的语法,我需要您的帮助
我一直试图弄清楚我在做什么,但在谷歌或PHP网站上找不到一个例子。我试过打印($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 = "";
}
?>
副标题。“
”;
echo$字幕;
/*检查所有条目并将其解析为HTML*/
foreach($xml->entry as$entry){
/*检索发布日期*/
$xmlDate=$entry->published;
/*将XML时间戳转换为PHP时间戳*/
$phpDate=新的日期时间(substr($xmlDate,0,19));
/*将PHP时间戳格式化为人类能够理解的格式*/
$pubDate.=$phpDate->format('l\,F j\,Y h:i A');
如果($entry->title==“”)
{
$entryTitle.=“无标题”;
}
echo$entry->title;
/*挑选每个条目并将每个XML树节点解析为一篇HTML就绪的博客文章*/
$html.=''.$entry->title.|“$pubDate.“$entry->content.
”;
/*将HTML打印到网页*/
echo$html;
/*将变量设置回空字符串,以便在重复时不会重复数据*/
$html=“”;
$pubDate=“”;
}
?>
第1部分:echo$xml->asXML()代码>-
第2部分:
根据您对第1部分的评论,我不确定XML是否已完全加载。如果尝试以这种方式加载,它将显示所有XML数据。simplexml\u load\u文件返回一个simplexmlement,因此:
print_r($xml);
将显示其次要对象和数组
调整后,可以调用$xml->asXML(“filename.xml”)正如蒂姆·威瑟斯所指出的。第一部分
这仍然不是我想要的,而是一种非常整洁和有组织的方式来响应xml数据:
$url = "http://www.blogger.com/feeds/6552111825067891333/posts/default";
$xml = simplexml_load_file($url);
echo '<pre>';
print_r($xml);
工作完美。对于字符串比较,我发现您可以简单地将其转换为字符串。就我而言,这是不必要的。根据php
手册:
echo $xml->asXML();
然后,如果要回显
结果:
$xml->asXML('blog.xml');
或将xml保存到文件中:
参考资料
第1部分:我尝试了asXML命令,但它仍然提供了xml的简化版本。第二部分:根据答案,我应该检查它是否为空。然后我倾向于推断我可以用字符串填充这个可能为空的元素。你同意吗?如果是的话,你能给我一个关于使用哪个函数的提示吗?谢谢蒂姆。现在在页面上打印未桥接的xml怎么样?检验差异;以下是虚拟博客页面:。RSS提要就在这里:事实上,你上次的编辑并没有像我想象的那样解决这个问题。我添加了:$feedUrl=“=file\u get\u contents($feedUrl);$xml=newsimplexmlement($rawFeed);然后我尝试了print\f、echo和echo$xml->asXML(尝试了各种变量)。我仍然得到了xml的简短版本。另外,您为第2部分提供的语法也不起作用。谢谢您的时间。
$xml = new SimpleXMLElement($string);
.
.
.
echo $xml->asXML();
$xml->asXML('blog.xml');