2012-05-08 15 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はあなたの友達です:)のようなもの:私は上記のXPathはなし(正確に右であるか分からない

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

それを試して)しかしそのようなもの。これは、あなたが常に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することによって達成することができます。これは、名前空間があるため原子名前空間の登録を必要とする作業なので、おそらくよりのようなものです: '文字列( // atom:link [@ rel = "next"]/@ href) '。 – hakre

+0

ありがとうございました。私は本当に座ってxPathを使用しなければなりません。 – justanotherdeveloper

1

これらの同じ例ではタイトル

$title=$xp->evaluate('string(//atom:content/m:properties/d:Title)'); 
関連する問題