0

在以下 xml 文件中,如何使用 PHP 解析“media:text”和“media:credit”和“media:content”属性值..??

<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>
4

2 回答 2

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);
    }
}
于 2013-10-14T08:29:34.813 回答
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";
    }

}``
于 2013-10-28T16:37:00.487 回答