回声simplexml对象 [英] echo simplexml object

查看:46
本文介绍了回声simplexml对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题分为两个部分.

This question has two parts.

第1部分.昨天,我有一些代码可以从RSS提要中回显XML的全部内容.然后,我从php文档中删除了该文件,并保存了下来,这让我感到无所适从.

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.

我相信语法是这样的:

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

我再次尝试过,但是它不起作用,所以很明显我忘记了正确的语法,可以使用您的帮助,亲爱的stackoverflow问题解答者.

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.

我一直在努力弄清自己在做什么,但无法在Google或PHP网站上找到示例.我尝试了print_r($ url);命令,它给了我看起来好像是Feed的原子化版本的功能.我想要整个字符串,疣和所有.我意识到我可以在窗口中键入RSS链接并查看它,但是在进行编码和点头时将其放在我的PHP页面上很有帮助.

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.

第2部分,我之所以要重构它,主要是因为我试图从博客RSS中解析出节点,以便将其显示在私有域托管的网页上.我没有在其中一个虚拟帖子中添加标题,就发布了一个虚拟博客,发现格式设置问题很尴尬.

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

那根本没有用.

这是我用于处理博客的整个php脚本:

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部分

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

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);

第2部分

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

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.

这篇关于回声simplexml对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆