解析XML提要不起作用

时间:2013-01-30 08:56:19

标签: php xml

我正在尝试解析xml或rss文件中的数据。这有点乱。人们告诉我,这很容易,但我很挣扎。

首先,我希望在网站上显示广播节目新闻。我从外部源获取了rss feed和api,并且他们每天更新xml内容,我只需要获取文件并将其保存在我的服务器上,这样我就可以读取它了。 以下是rss的示例。

<?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>

我将此文件保存为tonight.xml,并在24小时后从使用fread()的PHP脚本更新。

我想要展示当晚正在播放的节目的标题,没有别的 我对MySQL很好,但我真的没有得到这个。我很困惑。
这是我的php

<?php

$thexml = simplexml_load_file("tonight.xml");

echo $thexml->getName() . "<br />";

foreach($thexml->children() as $child)
  {
  echo $child->getName() . ": " . $child . "<br />";
  }

当我测试它时,它不会打印正确的值。

2 个答案:

答案 0 :(得分:2)

使用SimpleXMLElement::xpath()功能的快速解决方案:

$thexml = simplexml_load_file("tonight.xml");

foreach ($thexml->xpath('//item/title') as $title) {
    echo $title, "<br>\n";
}

您可以在此处找到更多Xpath资源:

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

那应该有用

你会得到它,只需专注于填补你在知识中找到的所有空白,你就会到达那里。

在这里阅读DomDocument和DomXPath PHP手册:

答案 1 :(得分:1)

如果您看到$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;
}