我无法解析此类代码:
<media:thumbnail url="http://images.indianexpress.com/2015/07/man-u-reuters_t.jpg?w=450" /> <media:content url="http://images.indianexpress.com/2015/07/man-u-reuters_t.jpg?w=450" medium="image">
当我使用此代码时,
<?php
$html = "";
$xml = simplexml_load_file("sports.xml");
foreach ($xml->channel->item as $news)
{
// echo $news->title;
$ns_media = $news->children("http://search.yahoo.com/mrss/");
echo $ns_media->content->title . "<br/>"; // Displays "<media:content>"
}
我的输出为:
Man-u-reuters_t
IanBellReuters-T
dutee-body2
Dutee-T
duleeptrophy-T
EnglandReuters-T
AzharAliAP-T
PakistanAP-T
AndersonReuters-T
KarunNairFile-T
ViratKohliPTI-T
kaneriaIEFile-M
PlatiniAP-T
asheslives
IndiaAustraliaT
ChelseaBarcelonaAPT
BradHaddinReutersT
ankeet-t
Arcehry-T
SardarSinghT
DuteeChandFB-T
CliveRice-T
ChrisRogersAP-T
ClarkeReuters-T
AlastairCookReuters-T
GangulyReuters-T
SachinTendulkarPTI-T
GangulyPTI-T
CliveRiceAP-T
ArcheryIEfile-T
PujaraPTIT
AshtonAgarTPTI
PeterNevillReuters-T
APJAbdulKalamT
IanBellReutersT
ChrisRogersAPT
hockey-small
dutee-chand-mi
DuteeChandT
mathew-small
ViratKohliPTIT
Sushil-Kumar-feat
Pro-Wrestling-League-feat
AnuragThakurT
SSreesanthPTIT
LalitModiT
Anirban-Lahiri-f
India-hockey-f
srk_express-file_t
Chris-Gayle-f
Viru_pti_t
Sergio-Romero_reuters_t
SachinPTI-T
SREESANTH_PTI_T
SSreesanthPTI-T
Srinivasan-T
TourdeFranceReuters-T
SriLankaReuters-T
cricket
SreesanthPTI-T
VettelReuters-T
WarneReuters-T
MarkWoodReuters-T
Misbah-ul-Haq-F
S-Sreesanth-f
yUVRAJ-_PTI_T
Sreesanth-f
abd_facebook_t
Yuvraj-Singh-f
Stuart-Broad-m
Chris-Rogers-f
Manchester-_reuters_t
kabaddi-thumb
Oltmans-T
ajit-thumb
sreesanth-thumb
Paul-Van-Ass_t
sreesanth1-thumb
Sreesanth_pti_t
Rahul-Dravid_pti_t
Amit-Mishra_pti-t
Lewis-Hamilton-F
Roelant-Oltman-MM
S-Sreesanth-f
bcci-thumb
Sourav-Ganguly_pti_t
SreesanthFileT
Bangladesh-f
Lewis-Hamilton-f
Sreesanth
Pakistan-f
Dale-Steyn_twitter_t
Usain-Bolt-f
Dhoni-f
Gayle_reuters_t
Usain-usa_t
Kohli-m
golf-thumb
Hockey-thumb
BCCI-thumb
felix-thumb
Didier-Drogba-f
VijenderSinghPTIT
RonaldoReutersT
Van-Ass-f
PragyanOjhaLT
SergioPerezAPT
IndiahockeyT
Saina-Nehwal-feat
MariaSharapovaT
Chung-Mong-joon-feat
Afganistan-feat
Bangladesh-feat
ShoaibSaniaLT
SureshRainaAPT
jaglan-480
mishra-thumb
oltmans-thumb
England-team-feat
Ojha-t
D-Steyn-main
AxarPatelT
BCCI-feat
Anurag-Thakur_t
SureshRainaT
Usain-Bolt-feat
MSDHoniT
ViratKohliPTIT
AmitMishraT
Anurag-Thakur-feat
ViratKohliFileTt
TamimIqbalAPT
ViratKohliT
raviICC-T
YurajSingh-T
ABdevilliers-T
BangladeshAP-t
DhoniAP-T
ShehzadReuters-T
ViratKohli-T
RahulPujaraBCCI-T
dhyan
YounisAP-T
Sangram-Singh-feat
ISl-feat
MahmudullahAP-T
Virat-Kohli_pti_m
Usain-Bolt-feat
Salman-Khan-feat
Yasir-Shah-feat
VVS-laxman_pti_t
Ashwin_facebook_t
Patil_pti_t
Virat-Kohli-ap-t
Sania-Mirza_facebook_t
Bairstow-ap-t
bcci-thumb
Virbhadra-Singh-thumb
MohitChhillar
VikasKrishan-T
yuri-small
IndiaHockeyVanAssPTIT
Sarfaraz-T
Cricket-T
Kevin-Pietersen_AP_2-M
SterlingReuters-T
JonnyBairstowReuters-T
Matthew-Wade-feat
BianchifuneralReuters-T
LehmannReuters-T
Praful-Patel-feat
mustafizurAP-T
Cheteshwar-Pujara--feat
Varun-Aaron-feat
Pele-feat
EdenGardensFileT
mickfanningT
RahulDravidT
SachinTendulkarWarneT
Jeffrey-WebbAPT
ChrisRogersReutersT
ZachJohnsonReutersT
ZachJohnsonReuters-T
JKCA
AnuragThakurPTLT
Paltan-T
Van-Ass_t
hockey
YounisKhanAP-T
RahulDravidPTI-T
UsmanAP-T
FIFAReuters-T
BlatterReuters-T
SaniaMirzaPTI-T
Harbhajan_singh_reuters_t
Central-Zone-feat
Rajiv-Shukla-feat
Paul-Van-Ass_t
Ganguly_pti_t
Sri-Lanka-Riots
我无法解析&lt; media:content url =“”&gt ;.
Stack Overflow问题 How to get media:content with SimpleXML 无效。
答案 0 :(得分:0)
我前一段时间写了这篇文章来解析BBC的RSS源......
$rssItems = $xml->channel->item;
foreach($rssItems as $rssItem){
if(is_array(($rssItem->xpath('media:thumbnail')))){
// If there are.. ..get them as an Array()
$mediaArray = $rssItem->xpath('media:thumbnail');
// Select the last Array item
$media = end($mediaArray);
// Build some variables to use in a string
$src = $media->attributes()->url;
$width = $media->attributes()->width.'px';
// Build a HTML string using the variables
$imageString = "<img src='$src' width='$width' />";
}
}