2013-10-14 28 views
0

xml名稱空間屬性解析如何使用PHP解析「media:text」和「media:credit」和「media:content」屬性值。在以下xml文件中使用php

<item> 
<title>Tom Tremendous: Brady rallies Patriots over Saints</title> 
<description> 
<p>With Gillette Stadium nearly half-empty, the fans figuring the New England Patriots had thrown away their last chance, Tom Brady delivered a drive to savor Sunday.</p> 
</description> 
<link> 
http://news.yahoo.com/tom-tremendous-brady-rallies-patriots-over-saints-011721864--spt.html 
</link> 
<pubDate>Sun, 13 Oct 2013 21:17:21 -0400</pubDate> 
<source url="http://www.ap.org/">Associated Press</source> 
<guid isPermaLink="false"> 
tom-tremendous-brady-rallies-patriots-over-saints-011721864--spt 
</guid> 
<media:content url="http://l2.yimg.com/bt/api/res/1.2/J7A0cSqro3niTyUB7bbLJQ--/YXBwaWQ9eW5ld3M7Zmk9ZmlsbDtoPTg2O3E9NzU7dz0xMzA-/http://media.zenfs.com/en_us/News/ap_webfeeds/183a1f218a441f22400f6a7067003712.jpg" type="image/jpeg" width="130" height="86"/> 
<media:text type="html"> 
</media:text> 
<media:credit role="publishing company"/> 
</item> 

回答

0
you can parse it like this when you have space url in the xml file usage $namespaceurl = your namespace value 

<?php 
$xml = '<item> 
<title>Tom Tremendous: Brady rallies Patriots over Saints</title> 
<description> 
<p>With Gillette Stadium nearly half-empty, the fans figuring the New England Patriots had thrown away their last chance, Tom Brady delivered a drive to savor Sunday.</p> 
</description> 
<link> 
http://news.yahoo.com/tom-tremendous-brady-rallies-patriots-over-saints-011721864--spt.html 
</link> 
<pubDate>Sun, 13 Oct 2013 21:17:21 -0400</pubDate> 
<source url="http://www.ap.org/">Associated Press</source> 
<guid isPermaLink="false"> 
tom-tremendous-brady-rallies-patriots-over-saints-011721864--spt 
</guid> 
<content url="http://l2.yimg.com/bt/api/res/1.2/J7A0cSqro3niTyUB7bbLJQ--/YXBwaWQ9eW5ld3M7Zmk9ZmlsbDtoPTg2O3E9NzU7dz0xMzA-/http://media.zenfs.com/en_us/News/ap_webfeeds/183a1f218a441f22400f6a7067003712.jpg" type="image/jpeg" width="130" height="86"/> 
<text type="html"> 
</text> 
<credit role="publishing company"/> 
</item>';enter code here 
$xmlDOc = new SimpleXmlElement($xml); 
if(count($xmlDOc)){ 
    $xmlDOc->registerXPathNamespace('media', '$namespaceurl'); 
    $result = $xmlDOc->xpath("//media:text"); 
    foreach ($result as $entry){ 
     var_dump($entry); 
    } 
} 

if you don't have namespace in the xml doc use like ths 


<?php 
$xml = '<item> 
<title>Tom Tremendous: Brady rallies Patriots over Saints</title> 
<description> 
<p>With Gillette Stadium nearly half-empty, the fans figuring the New England Patriots had thrown away their last chance, Tom Brady delivered a drive to savor Sunday.</p> 
</description> 
<link> 
http://news.yahoo.com/tom-tremendous-brady-rallies-patriots-over-saints-011721864--spt.html 
</link> 
<pubDate>Sun, 13 Oct 2013 21:17:21 -0400</pubDate> 
<source url="http://www.ap.org/">Associated Press</source> 
<guid isPermaLink="false"> 
tom-tremendous-brady-rallies-patriots-over-saints-011721864--spt 
</guid> 
<content url="http://l2.yimg.com/bt/api/res/1.2/J7A0cSqro3niTyUB7bbLJQ--/YXBwaWQ9eW5ld3M7Zmk9ZmlsbDtoPTg2O3E9NzU7dz0xMzA-/http://media.zenfs.com/en_us/News/ap_webfeeds/183a1f218a441f22400f6a7067003712.jpg" type="image/jpeg" width="130" height="86"/> 
<text type="html"> 
</text> 
<credit role="publishing company"/> 
</item>'; 
$xml = str_replace("<media:",'<md',$xml); //md is just differentiate 
$xml = str_replace("</media:","</md",$xml) 
$xmlDOc = new SimpleXmlElement($xml); 
if(count($xmlDOc)){ 
    $result = $xmlDOc->xpath("//mdtext"); 
    foreach ($result as $entry){ 
     var_dump($entry); 
    } 
} 
+0

我解析 「http://news.yahoo.com/rss/sports」 ......我可以獲取其他數據,<?PHP $ XML =使用simplexml_load_file('HTTP ://news.yahoo.com/rss/sports'); $ sitemap = $ xml-> channel; ($ sitemap-> children()as $ child){ echo $ child-> getName()。「
」; foreach($ child-> children()as $ subchild){ echo「--->」。$ subchild-> getName()。「:」。$ subchild。「
」; } } ?>但我不能得到如何獲得標籤.. –

0
here is the total working answer 

<?php 
$xml = file_get_contents("http://news.yahoo.com/rss/sports"); 
$xmlDOc = new SimpleXmlElement($xml); 
if(count($xmlDOc)){ 
    $xmlDOc->registerXPathNamespace('media', 'http://search.yahoo.com/mrss/'); 
    $result = $xmlDOc->xpath("//media:text/text()"); 

    while(list(, $node) = each($result)) { 
     echo $node,"\n"; 
    } 

}``