2016-08-15 3 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' /> 를 추출?

나는 이것을 추출하는 방법을 모르지만, 나는 쉽게 제목을 추출 할 수있다. 그러나 나는 링크 대체와 같은 것을 어떻게하는지, 조언 해 주셔서 감사한다.

$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 요소에 있으므로 loo 만 있습니다. p로 link을 입력하면 href을 얻을 수 있습니다.

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

데모 : https://eval.in/622605

0

당신은 당신이 필요로하는 노드를 추출하기 위해 XPath를 기능을 사용하는 것이 좋습니다 simple_xml를 사용하는 것처럼 다음의 경우,

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

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

위의 XPath는 모든 링크를 추출됩니다 XPath 문을보다 구체적으로 편집해야합니다.