从XML文件中抓取最后一个元素

时间:2012-05-08 20:20:19

标签: php xml-parsing

我有一个示例xml文件,如下所示。我可以有100个条目标签包含有关每个条目的信息。我所追求的是最后一个,然后关闭xml结构。

<feed xml:base="http://odata.me.com/v1/Catalog/" xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xmlns="http://www.w3.org/2005/Atom">
<title type="text">Videos</title>
<id>http://odata.me.com/v1/Catalog/Videos</id>  
 <link rel="self" title="Videos" href="Videos" />

 <entry>
    <id>http://odata.me.com/v1/Catalog/Videos('AEAB20400094')</id>
    <title type="text"></title>
    <updated>2012-05-08T19:20:08Z</updated>
    <author>
      <name />
    </author>
    <link rel="edit" title="VideoEf" href="Videos('AEAB20400094')" />
    <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/ContentProviderEntity" type="application/atom+xml;type=entry" title="ContentProviderEntity" href="Videos('AEAB20400094')/ContentProviderEntity" />
    <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/VideoVersionsCollection" type="application/atom+xml;type=feed" title="VideoVersionsCollection" href="Videos('AEAB20400094')/VideoVersionsCollection" />
    <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/VideoBuyLinksCollection" type="application/atom+xml;type=feed" title="VideoBuyLinksCollection" href="Videos('AEAB20400094')/VideoBuyLinksCollection" />
    <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/VideoMetadatasCollection" type="application/atom+xml;type=feed" title="VideoMetadatasCollection" href="Videos('AEAB20400094')/VideoMetadatasCollection" />
    <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/VideoPoliciesCollection" type="application/atom+xml;type=feed" title="VideoPoliciesCollection" href="Videos('AEAB20400094')/VideoPoliciesCollection" />
    <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/GenresCollection" type="application/atom+xml;type=feed" title="GenresCollection" href="Videos('AEAB20400094')/GenresCollection" />
    <category term="Me.Data.EF.Entities.VideoEf" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" />
    <content type="application/xml">
      <m:properties>
        <d:Isrc>AEAB20400094</d:Isrc>
        <d:Title>Akhasmak Ah</d:Title>
        <d:ThumbnailFilename>03FE946D8DC4D4E82A1EA56DD9EB89DA.jpg</d:ThumbnailFilename>
        <d:ContentProviderID m:type="Edm.Int32">11</d:ContentProviderID>
        <d:DurationInSeconds m:type="Edm.Int32">17</d:DurationInSeconds>
        <d:CreationDate m:type="Edm.DateTime">2010-10-26T21:57:30.363</d:CreationDate>
        <d:ModifiedDate m:type="Edm.DateTime">2012-05-03T20:56:42.383</d:ModifiedDate>
        <d:StartDate m:type="Edm.DateTime">2009-07-13T00:00:00</d:StartDate>
        <d:EndDate m:type="Edm.DateTime" m:null="true" />
        <d:CopyrightLine>(P) 2002 The copyright in this audiovisual recording is owned by Relax-In/Megastar under exclusive license to EMI Music Arabia</d:CopyrightLine>
        <d:FilteredTitle>Akhasmak Ah</d:FilteredTitle>
        <d:ThumbnailUrl>http://cache.me.com/Content/MeImages/video/03FE946D8DC4D4E82A1EA56DD9EB89DA.jpg</d:ThumbnailUrl>
      </m:properties>
    </content>
</entry>
 <link rel="next" href="http://odata.me.com/v1/Catalog/Videos?$skiptoken='AUBM80800189'" />  
</feed>

有谁知道我怎么能抓住这最后一个元素..我希望得到href中的值。

哦,我在PHP5中这样做

由于

2 个答案:

答案 0 :(得分:2)

Xpath是你的朋友:)类似于:

//link[@rel='next']/@href

我不确定上面的xpath是完全正确的(没有尝试过)但是它就是这样的。它也基于这样的假设,即你之后的最后一个链接元素总是具有rel属性,其值为'next'

你需要使用一些php xpath库来使用它。一些PHP示例代码:

$doc = new DOMDocument();
$doc->loadXML($data);
$xp = new DOMXPath($doc);
$xp->registerNamespace('atom', 'http://www.w3.org/2005/Atom');
var_dump($xp->evaluate('string(//atom:link[@rel="next"]/@href)')) ;

答案 1 :(得分:1)

在这些相同的例子中,标题可以通过

来实现
$title=$xp->evaluate('string(//atom:content/m:properties/d:Title)');