Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

i just have problems with extract data from XML.

Content looks like this:

  <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>noreply@blogger.com</email>
      <gd:image rel='link here' width='16' height='16' src='link here' />
    </author>
    <thr:total>0</thr:total>
  </entry>

Ho to extract data form <link rel='alternate' type='text/html' href='I NEED THIS LINK' title='Podaj tytu? wpisu spintax' /> ?

I dont know how to extract this, i can easy extract title but i dont know how to do the same with LINK alternate, thanks for advice.

Here is what i have write:

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

Also i have try with:

$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'};
        }
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
182 views
Welcome To Ask or Share your Answers For Others

1 Answer

You are already in the entry element so just loop through the links and you can get the href.

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

Demo: https://eval.in/622605


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...