php 回声 simplexml 对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11006390/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
echo simplexml object
提问by RafaSashi
This question has two parts.
这个问题有两个部分。
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.
第 1 部分。昨天我有一些代码可以从 RSS 提要中回显 XML 的全部内容。然后我从我的 php 文档中删除它,保存在它上面,我完全踢自己。
I believe the syntax went something like this:
我相信语法是这样的:
$xml = simplexml_load_file($url);
echo $xml;
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.
我再次尝试,但它不起作用,所以显然我忘记了正确的语法,可以使用您的帮助,亲爱的 stackoverflow 问答者。
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.
我一直试图弄清楚我在做什么,但我无法在 Google 或 PHP 站点上找到示例。我试过 print_r($url); 命令,它给了我似乎是提要的雾化版本。我想要整个字符串,疣和所有。我意识到我可以在窗口中输入 RSS 链接并查看它,但是在我编码和点头时将它放在我的 PHP 页面上很有帮助。
Part 2The 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.
第 2 部分我想重建它的主要原因是因为我试图从博客 RSS 中解析节点,以便将其显示在托管在私有域上的网页上。我发布了一个虚拟博客,并在我未能为其中一个虚拟帖子添加标题时发现了一个尴尬的格式故障。
So what does one do in this situation? I tried a little:
那么在这种情况下怎么办呢?我试了一下:
if(entry->title == "")
{$entryTitle = "untitled";}
That did not work at all.
那根本行不通。
Here's my entire php script for the handling of the blog:
这是我用于处理博客的整个 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 = "";
}
?>
采纳答案by RafaSashi
Part 1
第1部分
This is still not exactly what I wanted, but rather a very tidy and organized way of echoing the xml data:
这仍然不是我想要的,而是一种非常整洁和有组织的回显 xml 数据的方式:
$url = "http://www.blogger.com/feeds/6552111825067891333/posts/default";
$xml = simplexml_load_file($url);
echo '<pre>';
print_r($xml);
Part 2
第2部分
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:
我必须让 firephp 运行,这样我才能确切地看到当 php 到达一个没有博客标题的条目时遇到了哪些元素。最终它是一个空数组。因此,简单的:
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.
完美地工作。对于字符串比较,我发现您可以简单地将其转换为字符串。就我而言,这是不必要的。
回答by RafaSashi
According to the phpmanual:
根据php手册:
$xml = new SimpleXMLElement($string);
.
.
.
then if you want to echothe result:
那么如果你想要echo结果:
echo $xml->asXML();
or save the xml to a file:
或将 xml 保存到文件中:
$xml->asXML('blog.xml');
References
参考
回答by RafaSashi
The simplexml_load_filereturns an SimpleXMLElement, so:
在simplexml_load_file返回的SimpleXMLElement对象,所以:
print_r($xml);
will show its minor objectsand arrays.
将显示其次要对象和数组。
After your tweaks you can call $xml->asXML("filename.xml");as @Tim Withers pointed out.
调整后,您可以调用$xml->asXML("filename.xml"); 正如@Tim Withers 指出的那样。
回答by Tim Withers
Part 1: echo $xml->asXML();- http://www.php.net/manual/en/simplexmlelement.asxml.php
第 1 部分:echo $xml->asXML();- http://www.php.net/manual/en/simplexmlelement.asxml.php
Part 2: php SimpleXML check if a child exists
第 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>";
NoteI would probably load the url like this:
请注意,我可能会像这样加载网址:
$feedUrl = 'http://www.blogger.com/feeds/6552111825067891333/posts/default';
$rawFeed = file_get_contents($feedUrl);
$xml = new SimpleXmlElement($rawFeed);
Based on your comment in regards to part 1, I am not sure if the XML is being loaded completely. If you try loading it this way, it shoulddisplay all the XML data.
根据您对第 1 部分的评论,我不确定 XML 是否已完全加载。如果您尝试以这种方式加载它,它应该显示所有 XML 数据。

