通过属性在同一行上的多个节点的简单XML解析器

时间:2013-07-30 02:28:17

标签: php xml codeigniter xml-parsing simplexml

我不是一个出色的网络开发人员,我需要从amazon api feed中获取每个YouTube视频的链接。每个条目都有多个“链接”元素,我需要抓取<link rel="alternate">节点。

以下是我正在解析的内容:

 <?xml version='1.0' encoding='UTF-8'?>
<feed xmlns="http://www.w3.org/2005/Atom" xmlns:media="http://search.yahoo.com/mrss/" xmlns:openSearch="http://a9.com/-/spec/opensearchrss/1.0/" xmlns:gd="http://schemas.google.com/g/2005" xmlns:gml="http://www.opengis.net/gml" xmlns:yt="http://gdata.youtube.com/schemas/2007" xmlns:georss="http://www.georss.org/georss">
    <id>http://gdata.youtube.com/feeds/api/playlists/PL07B32D2A9CDF22DE</id>
    <updated>2012-08-02T23:50:11.000Z</updated>
    <category scheme="http://schemas.google.com/g/2005#kind" term="http://gdata.youtube.com/schemas/2007#playlist" />
    <title type="text">Julio Jones: Falcons Wide Receiver</title>
    <subtitle type="text" />
    <logo>http://www.youtube.com/img/pic_youtubelogo_123x63.gif</logo>
    <link rel="alternate" type="text/html" href="http://www.youtube.com/playlist?list=PL07B32D2A9CDF22DE" />
    <link rel="http://schemas.google.com/g/2005#feed" type="application/atom+xml" href="http://gdata.youtube.com/feeds/api/playlists/PL07B32D2A9CDF22DE" />
    <link rel="http://schemas.google.com/g/2005#batch" type="application/atom+xml" href="http://gdata.youtube.com/feeds/api/playlists/PL07B32D2A9CDF22DE/batch" />
    <link rel="self" type="application/atom+xml" href="http://gdata.youtube.com/feeds/api/playlists/PL07B32D2A9CDF22DE?start-index=1&amp;max-results=3" />
    <link rel="next" type="application/atom+xml" href="http://gdata.youtube.com/feeds/api/playlists/PL07B32D2A9CDF22DE?start-index=4&amp;max-results=3" />
    <author>
        <name>Atlanta Falcons</name>
        <uri>http://gdata.youtube.com/feeds/api/users/AtlantaFalconsNFL</uri>
    </author>
    <generator version="2.1" uri="http://gdata.youtube.com">YouTube data API</generator>
    <openSearch:totalResults>28</openSearch:totalResults>
    <openSearch:startIndex>1</openSearch:startIndex>
    <openSearch:itemsPerPage>3</openSearch:itemsPerPage>
    <media:group>
        <media:content url="http://www.youtube.com/p/PL07B32D2A9CDF22DE" type="application/x-shockwave-flash" yt:format="5" />
        <media:thumbnail url="http://i.ytimg.com/vi/Rm5tLcAxXAs/default.jpg" height="90" width="120" yt:name="default" />
        <media:thumbnail url="http://i.ytimg.com/vi/Rm5tLcAxXAs/mqdefault.jpg" height="180" width="320" yt:name="mqdefault" />
        <media:thumbnail url="http://i.ytimg.com/vi/Rm5tLcAxXAs/hqdefault.jpg" height="360" width="480" yt:name="hqdefault" />
        <media:title type="plain">Julio Jones: Falcons Wide Receiver</media:title>
    </media:group>
    <yt:playlistId>PL07B32D2A9CDF22DE</yt:playlistId>
    <entry>
        <id>http://gdata.youtube.com/feeds/api/playlists/PL07B32D2A9CDF22DE/PLTL9ir3vNAXsq-4gxYn2Tr2d2FeqOBspw</id>
        <published>2012-08-02T23:49:43.000Z</published>
        <updated>1970-01-01T00:00:00.000Z</updated>
        <category scheme="http://schemas.google.com/g/2005#kind" term="http://gdata.youtube.com/schemas/2007#video" />
        <category scheme="http://gdata.youtube.com/schemas/2007/categories.cat" term="Sports" label="Sports" />
        <title type="text">WR Julio Jones Soaking Up Veteran Knowledge</title>
        <content type="text">It's widely known that Roddy White has had a huge impact on the development of Julio Jones, but another perrenial Pro Bowler on the other side of the line of scrimmage is helping groom the second-year rising star.</content>
        <link rel="alternate" type="text/html" href="http://www.youtube.com/watch?v=fUdXu_BT7zQ&amp;feature=youtube_gdata" />
        <link rel="http://gdata.youtube.com/schemas/2007#video.responses" type="application/atom+xml" href="http://gdata.youtube.com/feeds/api/videos/fUdXu_BT7zQ/responses" />
        <link rel="http://gdata.youtube.com/schemas/2007#video.related" type="application/atom+xml" href="http://gdata.youtube.com/feeds/api/videos/fUdXu_BT7zQ/related" />
        <link rel="http://gdata.youtube.com/schemas/2007#mobile" type="text/html" href="http://m.youtube.com/details?v=fUdXu_BT7zQ" />
        <link rel="related" type="application/atom+xml" href="http://gdata.youtube.com/feeds/api/videos/fUdXu_BT7zQ" />
        <link rel="self" type="application/atom+xml" href="http://gdata.youtube.com/feeds/api/playlists/PL07B32D2A9CDF22DE/PLTL9ir3vNAXsq-4gxYn2Tr2d2FeqOBspw" />
        <author>
            <name>atlantafalconsnfl</name>
            <uri>http://gdata.youtube.com/feeds/api/users/atlantafalconsnfl</uri>
        </author>
        <gd:comments>
            <gd:feedLink rel="http://gdata.youtube.com/schemas/2007#comments" href="http://gdata.youtube.com/feeds/api/videos/fUdXu_BT7zQ/comments" countHint="13" />
        </gd:comments>
        <georss:where>
            <gml:Point>
                <gml:pos>34.1851 -83.92518</gml:pos>
            </gml:Point>
        </georss:where>
        <yt:hd />
        <media:group>
            <media:category label="Sports" scheme="http://gdata.youtube.com/schemas/2007/categories.cat">Sports</media:category>
            <media:content url="http://www.youtube.com/v/fUdXu_BT7zQ?version=3&amp;f=playlists&amp;app=youtube_gdata" type="application/x-shockwave-flash" medium="video" isDefault="true" expression="full" duration="143" yt:format="5" />
            <media:content url="rtsp://r2---sn-p5qlsu7s.c.youtube.com/CiULENy73wIaHAk071Pwu1dHfRMYDSANFEgGUglwbGF5bGlzdHMM/0/0/0/video.3gp" type="video/3gpp" medium="video" expression="full" duration="143" yt:format="1" />
            <media:content url="rtsp://r2---sn-p5qlsu7s.c.youtube.com/CiULENy73wIaHAk071Pwu1dHfRMYESARFEgGUglwbGF5bGlzdHMM/0/0/0/video.3gp" type="video/3gpp" medium="video" expression="full" duration="143" yt:format="6" />
            <media:description type="plain">It's widely known that Roddy White has had a huge impact on the development of Julio Jones, but another perrenial Pro Bowler on the other side of the line of scrimmage is helping groom the second-year rising star.</media:description>
            <media:keywords />
            <media:player url="http://www.youtube.com/watch?v=fUdXu_BT7zQ&amp;feature=youtube_gdata_player" />
            <media:thumbnail url="http://i1.ytimg.com/vi/fUdXu_BT7zQ/0.jpg" height="360" width="480" time="00:01:11.500" />
            <media:thumbnail url="http://i1.ytimg.com/vi/fUdXu_BT7zQ/1.jpg" height="90" width="120" time="00:00:35.750" />
            <media:thumbnail url="http://i1.ytimg.com/vi/fUdXu_BT7zQ/2.jpg" height="90" width="120" time="00:01:11.500" />
            <media:thumbnail url="http://i1.ytimg.com/vi/fUdXu_BT7zQ/3.jpg" height="90" width="120" time="00:01:47.250" />
            <media:title type="plain">WR Julio Jones Soaking Up Veteran Knowledge</media:title>
            <yt:duration seconds="143" />
        </media:group>
        <gd:rating average="5.0" max="5" min="1" numRaters="13" rel="http://schemas.google.com/g/2005#overall" />
        <yt:recorded>2012-08-02</yt:recorded>
        <yt:statistics favoriteCount="0" viewCount="4240" />
        <yt:description>It's widely known that Roddy White has had a huge impact on the development of Julio Jones, but another perrenial Pro Bowler on the other side of the line of scrimmage is helping groom the second-year rising star.</yt:description>
        <yt:position>1</yt:position>
    </entry>
    <entry>
        <id>http://gdata.youtube.com/feeds/api/playlists/PL07B32D2A9CDF22DE/PLTL9ir3vNAXsjKrxurClwVOmQ3lb41H7L</id>
        <published>2012-08-01T16:25:32.000Z</published>
        <updated>1970-01-01T00:00:00.000Z</updated>
        <category scheme="http://schemas.google.com/g/2005#kind" term="http://gdata.youtube.com/schemas/2007#video" />
        <category scheme="http://gdata.youtube.com/schemas/2007/categories.cat" term="Sports" label="Sports" />
        <title type="text">Tuesday's Training Camp: Quick Hits</title>
        <content type="text">In the final practice before the first day off of 2012 Russell Falcons Training Camp, the new offensive and defensive schemes continued to gel.</content>
        <link rel="alternate" type="text/html" href="http://www.youtube.com/watch?v=Rm5tLcAxXAs&amp;feature=youtube_gdata" />
        <link rel="http://gdata.youtube.com/schemas/2007#video.responses" type="application/atom+xml" href="http://gdata.youtube.com/feeds/api/videos/Rm5tLcAxXAs/responses" />
        <link rel="http://gdata.youtube.com/schemas/2007#video.related" type="application/atom+xml" href="http://gdata.youtube.com/feeds/api/videos/Rm5tLcAxXAs/related" />
        <link rel="http://gdata.youtube.com/schemas/2007#mobile" type="text/html" href="http://m.youtube.com/details?v=Rm5tLcAxXAs" />
        <link rel="related" type="application/atom+xml" href="http://gdata.youtube.com/feeds/api/videos/Rm5tLcAxXAs" />
        <link rel="self" type="application/atom+xml" href="http://gdata.youtube.com/feeds/api/playlists/PL07B32D2A9CDF22DE/PLTL9ir3vNAXsjKrxurClwVOmQ3lb41H7L" />
        <author>
            <name>atlantafalconsnfl</name>
            <uri>http://gdata.youtube.com/feeds/api/users/atlantafalconsnfl</uri>
        </author>
        <gd:comments>
            <gd:feedLink rel="http://gdata.youtube.com/schemas/2007#comments" href="http://gdata.youtube.com/feeds/api/videos/Rm5tLcAxXAs/comments" countHint="1" />
        </gd:comments>
        <georss:where>
            <gml:Point>
                <gml:pos>34.1851 -83.92518</gml:pos>
            </gml:Point>
        </georss:where>
        <yt:hd />
        <media:group>
            <media:category label="Sports" scheme="http://gdata.youtube.com/schemas/2007/categories.cat">Sports</media:category>
            <media:content url="http://www.youtube.com/v/Rm5tLcAxXAs?version=3&amp;f=playlists&amp;app=youtube_gdata" type="application/x-shockwave-flash" medium="video" isDefault="true" expression="full" duration="79" yt:format="5" />
            <media:content url="rtsp://r4---sn-p5qlsu7e.c.youtube.com/CiULENy73wIaHAkLXDHALW1uRhMYDSANFEgGUglwbGF5bGlzdHMM/0/0/0/video.3gp" type="video/3gpp" medium="video" expression="full" duration="79" yt:format="1" />
            <media:content url="rtsp://r4---sn-p5qlsu7e.c.youtube.com/CiULENy73wIaHAkLXDHALW1uRhMYESARFEgGUglwbGF5bGlzdHMM/0/0/0/video.3gp" type="video/3gpp" medium="video" expression="full" duration="79" yt:format="6" />
            <media:description type="plain">In the final practice before the first day off of 2012 Russell Falcons Training Camp, the new offensive and defensive schemes continued to gel.</media:description>
            <media:keywords />
            <media:player url="http://www.youtube.com/watch?v=Rm5tLcAxXAs&amp;feature=youtube_gdata_player" />
            <media:thumbnail url="http://i1.ytimg.com/vi/Rm5tLcAxXAs/0.jpg" height="360" width="480" time="00:00:39.500" />
            <media:thumbnail url="http://i1.ytimg.com/vi/Rm5tLcAxXAs/1.jpg" height="90" width="120" time="00:00:19.750" />
            <media:thumbnail url="http://i1.ytimg.com/vi/Rm5tLcAxXAs/2.jpg" height="90" width="120" time="00:00:39.500" />
            <media:thumbnail url="http://i1.ytimg.com/vi/Rm5tLcAxXAs/3.jpg" height="90" width="120" time="00:00:59.250" />
            <media:title type="plain">Tuesday's Training Camp: Quick Hits</media:title>
            <yt:duration seconds="79" />
        </media:group>
        <gd:rating average="5.0" max="5" min="1" numRaters="4" rel="http://schemas.google.com/g/2005#overall" />
        <yt:recorded>2012-08-31</yt:recorded>
        <yt:statistics favoriteCount="0" viewCount="1233" />
        <yt:description>In the final practice before the first day off of 2012 Russell Falcons Training Camp, the new offensive and defensive schemes continued to gel.</yt:description>
        <yt:position>2</yt:position>
    </entry>
    <entry>
        <id>http://gdata.youtube.com/feeds/api/playlists/PL07B32D2A9CDF22DE/PLTL9ir3vNAXsGRJhgHr8YqbDzzp1um-HX</id>
        <published>2012-08-01T16:25:32.000Z</published>
        <updated>1970-01-01T00:00:00.000Z</updated>
        <category scheme="http://schemas.google.com/g/2005#kind" term="http://gdata.youtube.com/schemas/2007#video" />
        <category scheme="http://gdata.youtube.com/schemas/2007/categories.cat" term="Sports" label="Sports" />
        <title type="text">Scenes from Monday: Quick Hits</title>
        <content type="text">Line blocking and footwork drills, receivers working on their routes and various special teams reps highlight this video glimpse at Monday afternoon's training camp practice.</content>
        <link rel="alternate" type="text/html" href="http://www.youtube.com/watch?v=5bESx10p3_Y&amp;feature=youtube_gdata" />
        <link rel="http://gdata.youtube.com/schemas/2007#video.responses" type="application/atom+xml" href="http://gdata.youtube.com/feeds/api/videos/5bESx10p3_Y/responses" />
        <link rel="http://gdata.youtube.com/schemas/2007#video.related" type="application/atom+xml" href="http://gdata.youtube.com/feeds/api/videos/5bESx10p3_Y/related" />
        <link rel="http://gdata.youtube.com/schemas/2007#mobile" type="text/html" href="http://m.youtube.com/details?v=5bESx10p3_Y" />
        <link rel="related" type="application/atom+xml" href="http://gdata.youtube.com/feeds/api/videos/5bESx10p3_Y" />
        <link rel="self" type="application/atom+xml" href="http://gdata.youtube.com/feeds/api/playlists/PL07B32D2A9CDF22DE/PLTL9ir3vNAXsGRJhgHr8YqbDzzp1um-HX" />
        <author>
            <name>atlantafalconsnfl</name>
            <uri>http://gdata.youtube.com/feeds/api/users/atlantafalconsnfl</uri>
        </author>
        <gd:comments>
            <gd:feedLink rel="http://gdata.youtube.com/schemas/2007#comments" href="http://gdata.youtube.com/feeds/api/videos/5bESx10p3_Y/comments" countHint="4" />
        </gd:comments>
        <georss:where>
            <gml:Point>
                <gml:pos>34.1851 -83.92518</gml:pos>
            </gml:Point>
        </georss:where>
        <yt:hd />
        <media:group>
            <media:category label="Sports" scheme="http://gdata.youtube.com/schemas/2007/categories.cat">Sports</media:category>
            <media:content url="http://www.youtube.com/v/5bESx10p3_Y?version=3&amp;f=playlists&amp;app=youtube_gdata" type="application/x-shockwave-flash" medium="video" isDefault="true" expression="full" duration="103" yt:format="5" />
            <media:content url="rtsp://r4---sn-p5qlsu7k.c.youtube.com/CiULENy73wIaHAn23yldxxKx5RMYDSANFEgGUglwbGF5bGlzdHMM/0/0/0/video.3gp" type="video/3gpp" medium="video" expression="full" duration="103" yt:format="1" />
            <media:content url="rtsp://r4---sn-p5qlsu7k.c.youtube.com/CiULENy73wIaHAn23yldxxKx5RMYESARFEgGUglwbGF5bGlzdHMM/0/0/0/video.3gp" type="video/3gpp" medium="video" expression="full" duration="103" yt:format="6" />
            <media:description type="plain">Line blocking and footwork drills, receivers working on their routes and various special teams reps highlight this video glimpse at Monday afternoon's training camp practice.</media:description>
            <media:keywords />
            <media:player url="http://www.youtube.com/watch?v=5bESx10p3_Y&amp;feature=youtube_gdata_player" />
            <media:thumbnail url="http://i1.ytimg.com/vi/5bESx10p3_Y/0.jpg" height="360" width="480" time="00:00:51.500" />
            <media:thumbnail url="http://i1.ytimg.com/vi/5bESx10p3_Y/1.jpg" height="90" width="120" time="00:00:25.750" />
            <media:thumbnail url="http://i1.ytimg.com/vi/5bESx10p3_Y/2.jpg" height="90" width="120" time="00:00:51.500" />
            <media:thumbnail url="http://i1.ytimg.com/vi/5bESx10p3_Y/3.jpg" height="90" width="120" time="00:01:17.250" />
            <media:title type="plain">Scenes from Monday: Quick Hits</media:title>
            <yt:duration seconds="103" />
        </media:group>
        <gd:rating average="5.0" max="5" min="1" numRaters="3" rel="http://schemas.google.com/g/2005#overall" />
        <yt:recorded>2012-07-30</yt:recorded>
        <yt:statistics favoriteCount="0" viewCount="1381" />
        <yt:description>Line blocking and footwork drills, receivers working on their routes and various special teams reps highlight this video glimpse at Monday afternoon's training camp practice.</yt:description>
        <yt:position>3</yt:position>
    </entry>
</feed>

我猜它是这样的:

foreach($xml->entry as $entry){
        //echo $entry->title . "<br>";
    //  echo count($entry->link) . "<br>";
    echo $entry->link[rel="alternate"];
    //  echo "<a href=\"". $entry->link->attribute() . "\">" . $entry->title . "</a>" . "<br>";
    }

但我不确定。任何帮助都会很棒!

我也很好奇如何将这些信息从mvc设置(codeigniter)传递到视图中作为显示最近视频的模型,但这不是一个非常紧迫的问题。

3 个答案:

答案 0 :(得分:0)

我会尝试像

这样的东西
if($entry->link['rel'] === 'alternate'){ echo etc... }

答案 1 :(得分:0)

使用xpath

$simplexml_load_string($x); // assume XML in $x
$xml->registerXPathNamespace('m', 'http://www.w3.org/2005/Atom');
$links = $xml->xpath("//m:link[@rel='alternate']/@href");

数组$links将包含href个属性的<link>个属性,其rel属性包含alternate。输出这样的值:

foreach ($links as $link) echo "$link <br />";

看到它有效:http://3v4l.org/BPueX

答案 2 :(得分:0)

我知道我对这个答案有点迟了:

foreach ($xml->entry as $entry){
        //For each loop determining how many videos to display based on how many
        //entries there are in the feed.
        $nameSpaces = $entry->getNameSpaces(true);
        $yt         = $entry->children($nameSpaces['yt']);
        $mediayt    = $entry->children($nameSpaces['media'])->group->children($nameSpaces['yt']);
            //Hooray for namespaces... not.
        $link       = $entry->link[0]['href'];
            //Grabs the first of four links. The third link is for mobile and 
            //has a cleaner URL but does not work with Swipebox.
        $id         = basename($entry->id);
            //Gets the id for each video.
        $title      = $entry->title;
            //Gets the title for each video.
        $published  = date('dS M Y', strtotime($entry->published));
            //Gets the published date of each video and displays it by
            //Day Month Year. If not set will display 31st Dec 1969
        $content    = $entry->content;
            //Gets the video description, if any.
        $ytRecorded = date('dS M Y', strtotime($yt->recorded));
            //Gets the recorded date of each video and displays it by
            //Day Month Year. If not set will display 31st Dec 1969
        $ytDuration = gmdate("i:s",(int) $mediayt->duration['seconds']);
            //Gets the video duration in seconds and convert it to
            //Minutes:Seconds(00:00)
        $ytViewCount= $yt->statistics['viewCount'];
            //This will get the view count for each video.
    }

如您所见,您不需要xpath,attributes()或任何tom-foolery来获取多个条目。另一方面,命名空间很荒谬。

您可以指定数组中的第一个[0],以及['href']之后的属性。