2012-05-08 43 views
0

我有一個如下所示的示例xml文件。我可以有100個條目標籤包含有關每個條目的信息。我所追求的是最終的結果,並在此之前關閉了xml結構。從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

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)')) ; 
+0

+1來實現:它的工作,需要的命名空間,因爲原子命名空間的註冊,所以可能更喜歡:'字符串( //原子:連結[@的rel = 「下一個」]/@ HREF)'。 – hakre

+0

謝謝你。我真的得屈服並使用xPath。 – justanotherdeveloper

1

在這些相同的例子標題可以通過

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