Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
203 views
in Technique[技术] by (71.8m points)

php - Extract value form XML

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>[email protected]</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

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

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...