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>

このファイルをtonight.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 />";
  }

テストすると、正しい値が出力されません。

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

それはうまくいくはずです

あなたはそれを得るでしょう、あなたの知識で見つけたすべてのギャップを埋めることに集中するだけで、そこにたどり着きます.

DomDocument と DomXPath については、こちらの PHP マニュアルを参照してください。

于 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 に答える