0

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

首先,我想让广播节目新闻在现场显示。我从外部来源获得了 rss 提要和 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>

我将此文件保存为 night.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 />";
  }

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

4

2 回答 2

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

那应该工作

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

在 PHP 手册中阅读 DomDocument 和 DomXPath:

于 2013-01-30T08:57:09.440 回答
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;
}
于 2013-01-30T09:04:50.133 回答