2016-08-15 9 views
-1

XMLからのデータ抽出に問題があります。値の抽出フォームXML

内容は次のようになります。

<entry> 
    <id>tag:blogger.com,1999:blog-227841964192642355.post-8872593151327492602</id> 
    <published>2016-08-15T01:56:00.001-07:00</published> 
    <updated>2016-08-15T01:56:36.304-07:00</updated> 
    <title type='text'>Podaj tytuł wpisu spintax</title> 
    <content type='html'>Treść wpisu spintax </content> 
    <link rel='replies' type='application/atom+xml' href='http://ecookingwithme.blogspot.com/feeds/8872593151327492602/comments/default' title='Komentarze do posta' /> 
    <link rel='replies' type='text/html' href='http://ecookingwithme.blogspot.com/2016/08/podaj-tytu-wpisu-spintax.html#comment-form' title='Komentarze (0)' /> 
    <link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/227841964192642355/posts/default/8872593151327492602' /> 
    <link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/227841964192642355/posts/default/8872593151327492602' /> 
    <link rel='alternate' type='text/html' href='I NEED THIS LINK' title='Podaj tytuł wpisu spintax' /> 
    <author> 
     <name>NAMEa</name> 
     <uri>link here</uri> 
     <email>[email protected]</email> 
     <gd:image rel='link here' width='16' height='16' src='link here' /> 
    </author> 
    <thr:total>0</thr:total> 
    </entry> 

ホーデータ形式<link rel='alternate' type='text/html' href='I NEED THIS LINK' title='Podaj tytuł wpisu spintax' /> を抽出するために?

私はこれを抽出する方法はわかりませんが、簡単にタイトルを抽出することはできますが、LINKの代わりにアドバイスをしても同じことをする方法はわかりません。ここで

は私が書き込みを持っているものです。

$file = "http://ecookingwithme.blogspot.com/atom.xml"; 
$xml = simplexml_load_file($file); 
echo $xml->entry->title; 

また、私がしようとしている:

$file = "http://ecookingwithme.blogspot.com/atom.xml"; 
$xml = simplexml_load_file($file); 

foreach ($xml->entry as $foo) { 
    foreach ($foo->link as $link) { 
     $type = (string) $link->attributes()->{'rel'}; 
     if ($type == 'rel') { 
      echo (string) $link->attributes()->{'href'}; 
     } 
    } 
} 
+0

次のいずれかの回答が問題を解決していますか? http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work – chris85

答えて

0

あなたがコード

の数行でこれを行うことができます simple_xmlが、定期的な DOMDocumentを使用していません
$file='http://ecookingwithme.blogspot.com/atom.xml'; 

$dom=new DOMDocument; 
$dom->load($file); 

$col=$dom->getElementsByTagName('link'); 
foreach($col as $node) echo $node->getAttribute('href').'<br />'; 
1

あなたは既にentryの要素になっていますので、 pをlinkに入力すると、hrefが得られます。

$sxml = new simplexmlelement($xml); 
foreach($sxml->link as $link) { 
    if($link['rel'] == 'alternate') { 
      echo $link['href']; 
    } 
} 

デモ:

$file = "http://ecookingwithme.blogspot.com/atom.xml"; 
$xml = simplexml_load_file($file); 

$links = $xml -> Xpath("//link/@href"); 

上記XPathはすべてのリンクを抽出します、場合:あなたはsimple_xmlを使用しているとしてhttps://eval.in/622605

0

あなたが必要なノードを抽出するためにそのXPathの機能を使用して検討するかもしれませんXPath文を編集するより具体的にする必要があります。