Php 解析XML提要不起作用
我正在尝试解析xml或rss文件中的数据。有点乱。人们告诉我,这意味着简单,但我挣扎了很多 首先,我想让广播节目的新闻显示在网站上。我从外部源获取rss提要和api,它们每天更新xml内容,我只需要获取文件并将其保存在服务器上,这样我就可以读取它。 下面是rss的一个示例Php 解析XML提要不起作用,php,xml,Php,Xml,我正在尝试解析xml或rss文件中的数据。有点乱。人们告诉我,这意味着简单,但我挣扎了很多 首先,我想让广播节目的新闻显示在网站上。我从外部源获取rss提要和api,它们每天更新xml内容,我只需要获取文件并将其保存在服务器上,这样我就可以读取它。 下面是rss的一个示例 <?xml version="1.0"?> <channel> <title>external source</title> <lin
<?xml version="1.0"?>
<channel>
<title>external source</title>
<link>http://www.externalsource.com/</link>
<description>external source</description>
<language>en-us</language>
<pubDate>Thu, Jun 3 2011 10:23:56 PDT</pubDate>
<ttl>60</ttl>
<docs>http://blogs.law.harvard.edu/tech/rss</docs>
<item>
<title>08:00 pm</title>
<link>http://www.externalsource.com</link>
</item>
<item>
<title>- Stephen Ace show</title>
<link>http://www.externalsource.com/stephen_ace</link>
<description>Stephens show</description>
</item>
<item>
<title>- Sarah Hardy show</title>
<link>http://www.externalsource.com/sarah_hardy</link>
<description> Sarah's first show in her new slot.</description>
</item>
<item>
<title>- Radio 4 evening show</title>
<link>http://www.externalsource.com/shows/id-453</link>
<description>Bill Grady is supported by co-host Lenny Hillroy</description>
</item>
<item>
<title>- Kiss music evening show will Sady</title>
<link>http://www.externalsource.com/shows/id-112</link>
<description>Sady presents the evening show here at Kiss.</description>
</item>
</channel>
外部源
http://www.externalsource.com/
外部源
美国英语
周四,2011年6月3日10:23:56 PDT
60
http://blogs.law.harvard.edu/tech/rss
晚上8:00
http://www.externalsource.com
-斯蒂芬·艾斯秀
http://www.externalsource.com/stephen_ace
斯蒂芬斯秀
-莎拉哈代秀
http://www.externalsource.com/sarah_hardy
莎拉在新位置的第一场演出。
-第四台晚间节目
http://www.externalsource.com/shows/id-453
比尔·格雷迪得到了联合主持人莱尼·希尔罗伊的支持
-亲吻音乐晚会威尔·萨迪
http://www.externalsource.com/shows/id-112
Sady在Kiss主持晚会。
我将此文件保存为nightly.xml,并通过使用fread()的PHP脚本24小时更新它
我只想展示当晚正在播放的节目的标题,没有其他内容。我可以用MySQL,但我真的不明白这一点。我被卡住了。
这是我的php
<?php
$thexml = simplexml_load_file("tonight.xml");
echo $thexml->getName() . "<br />";
foreach($thexml->children() as $child)
{
echo $child->getName() . ": " . $child . "<br />";
}
使用以下功能的快速解决方案:
$thexml = simplexml_load_file("tonight.xml");
foreach ($thexml->xpath('//item/title') as $title) {
echo $title, "<br>\n";
}
$thexml=simplexml\u load\u文件(“今晚.xml”);
foreach($thexml->xpath('//item/title')作为$title){
echo$title,“
\n”;
}
您可以在这里找到更多Xpath资源:
- ,PHP从今天起使用Xpath 1.0
SimpleXML旁边还有DOMDocument,它的姐姐:
libxml_use_internal_errors(true);
$dom = new DomDocument();
if ($dom->load('tonight.xml')) {
$xpath = new DomXPath($dom);
foreach ($xpath->query('//item/title') as $node) {
echo $node->nodeValue, "<br>\n";
}
}
libxml\u使用\u内部错误(true);
$dom=新的DomDocument();
if($dom->load('nightly.xml')){
$xpath=newdomxpath($dom);
foreach($xpath->query('//item/title')作为$node){
echo$node->nodeValue,“
\n”;
}
}
这应该行得通
你会明白的,只要专注于填补你在知识中发现的所有空白,你就会达到目的
请阅读PHP手册中的DomDocument和DOMDxpath:
使用以下功能的快速解决方案:
$thexml = simplexml_load_file("tonight.xml");
foreach ($thexml->xpath('//item/title') as $title) {
echo $title, "<br>\n";
}
$thexml=simplexml\u load\u文件(“今晚.xml”);
foreach($thexml->xpath('//item/title')作为$title){
echo$title,“
\n”;
}
您可以在这里找到更多Xpath资源:
- ,PHP从今天起使用Xpath 1.0
SimpleXML旁边还有DOMDocument,它的姐姐:
libxml_use_internal_errors(true);
$dom = new DomDocument();
if ($dom->load('tonight.xml')) {
$xpath = new DomXPath($dom);
foreach ($xpath->query('//item/title') as $node) {
echo $node->nodeValue, "<br>\n";
}
}
libxml\u使用\u内部错误(true);
$dom=新的DomDocument();
if($dom->load('nightly.xml')){
$xpath=newdomxpath($dom);
foreach($xpath->query('//item/title')作为$node){
echo$node->nodeValue,“
\n”;
}
}
这应该行得通
你会明白的,只要专注于填补你在知识中发现的所有空白,你就会达到目的
请阅读PHP手册中的DomDocument和DOMDxpath:
如果您观看您的$thexml
内容,您会看到以下内容:
print_r( $thexml );
SimpleXMLElement Object
(
[title] => external source
[link] => http://www.externalsource.com/
[description] => external source
[language] => en-us
[pubDate] => Thu, Jun 3 2011 10:23:56 PDT
[ttl] => 60
[docs] => http://blogs.law.harvard.edu/tech/rss
[item] => Array
(
[0] => SimpleXMLElement Object
(
[title] => 08:00 pm
[link] => http://www.externalsource.com
)
[1] => SimpleXMLElement Object
(
[title] => - Stephen Ace show
[link] => http://www.externalsource.com/stephen_ace
[description] => Stephens show
)
[2] => SimpleXMLElement Object
(
[title] => - Sarah Hardy show
[link] => http://www.externalsource.com/sarah_hardy
[description] => Sarah's first show in her new slot.
)
[3] => SimpleXMLElement Object
(
[title] => - Radio 4 evening show
[link] => http://www.externalsource.com/shows/id-453
[description] => Bill Grady is supported by co-host Lenny Hillroy
)
[4] => SimpleXMLElement Object
(
[title] => - Kiss music evening show will Sady
[link] => http://www.externalsource.com/shows/id-112
[description] => Sady presents the evening show here at Kiss.
)
)
)
您可以像这样迭代项数组中的所有元素:
foreach( $thexml->item as $item ) {
echo $item->title;
}
如果您观看$thexml
内容,您会看到以下内容:
print_r( $thexml );
SimpleXMLElement Object
(
[title] => external source
[link] => http://www.externalsource.com/
[description] => external source
[language] => en-us
[pubDate] => Thu, Jun 3 2011 10:23:56 PDT
[ttl] => 60
[docs] => http://blogs.law.harvard.edu/tech/rss
[item] => Array
(
[0] => SimpleXMLElement Object
(
[title] => 08:00 pm
[link] => http://www.externalsource.com
)
[1] => SimpleXMLElement Object
(
[title] => - Stephen Ace show
[link] => http://www.externalsource.com/stephen_ace
[description] => Stephens show
)
[2] => SimpleXMLElement Object
(
[title] => - Sarah Hardy show
[link] => http://www.externalsource.com/sarah_hardy
[description] => Sarah's first show in her new slot.
)
[3] => SimpleXMLElement Object
(
[title] => - Radio 4 evening show
[link] => http://www.externalsource.com/shows/id-453
[description] => Bill Grady is supported by co-host Lenny Hillroy
)
[4] => SimpleXMLElement Object
(
[title] => - Kiss music evening show will Sady
[link] => http://www.externalsource.com/shows/id-112
[description] => Sady presents the evening show here at Kiss.
)
)
)
您可以像这样迭代项数组中的所有元素:
foreach( $thexml->item as $item ) {
echo $item->title;
}
嘿,是的,我试过你给我的密码了。将签出DomDoc和XPath。谢谢,就像使用xpath一样,我认为它让我对nodes@HarryMason:我修复了答案中指向资源的链接,并将W3傻瓜链接替换为我们网站上更具洞察力的资源。Xpath非常有用,是的。花点时间去学习它,哪一个最好的办法就是玩它。嘿,是的,你试过你给的代码,而且很管用。将签出DomDoc和XPath。谢谢,就像使用xpath一样,我认为它让我对nodes@HarryMason:我修复了答案中指向资源的链接,并将W3傻瓜链接替换为我们网站上更具洞察力的资源。Xpath非常有用,是的。花点时间来学习它,最好的方法是玩它。哦,我明白了,所以请指向item对象,然后指向每个items title属性。我也很感谢您,但是正在阅读xpath并希望尝试更多地使用它,非常灵活哦,我看到了,请指向item对象,然后指向每个items title属性。我也非常感谢您,但是正在阅读xpath,希望尝试更灵活地使用它