2012-01-08 2 views
0

Я хочу, чтобы разобрать следующий XML и получить значение метаданных: описание с помощью PHPКак разобрать XML-файл с помощью PHP

Я знаю, как получить значение заголовка

$item_title = $x -> item($i) -> getElementsByTagName('title') -> item(0) -> childNodes -> item(0) -> nodeValue; 

, но не может использовать этот способ, чтобы получить метаданные: описание

<item> 
    <title>Jobs: Bullish Economy Confirmed?</title> 
    <metadata:title xmlns:metadata="http://search.cnbc.com/rss/2.0/modules/siteContentMetadata">Jobs: Bullish Economy Confirmed? 06 Jan 2012</metadata:title> 
    <description>Discussing whether the better than expected jobs number point to an economic rebound, and where to invest in this market, with Michael Farr, Farr, Miller, &amp; Washington president.</description> 
    <metadata:description xmlns:metadata="http://search.cnbc.com/rss/2.0/modules/siteContentMetadata"><![CDATA[<div class="rss_image" style="float:left;padding-right:10px;"><img border="0" vspace="0" hspace="0" width="93" src="http://thumbnails.cnbc.com/VCPS/Y2012/M01D06/3000066213/6ED1-KR-Jobs_sm.jpg"></div><div class="rss_abstract" style="font:Arial 12px;width:100%;float:left;clear:both">Discussing whether the better than expected jobs number point to an economic rebound, and where to invest in this market, with Michael Farr, Farr, Miller, &amp; Washington president.</div>]]></metadata:description> 
    <pubDate>Sat, 07 Jan 2012 00:24 GMT</pubDate> 
    <guid isPermaLink="false">http://www.cnbc.com//id/15840232?video=3000066213&amp;play=1</guid> 
    <link>http://www.cnbc.com//id/15840232?video=3000066213&amp;play=1</link> 
</item> 

Оригинальный XML файл ссылка

http://www.cnbc.com/id/19838222/device/rss/rss.xml 

ответ

1

Использование SimpleXML

$xmlstr='<item> 
<title>Jobs: Bullish Economy Confirmed?</title> 
<metadata:title xmlns:metadata="http://search.cnbc.com/rss/2.0/modules/siteContentMetadata">Jobs: Bullish Economy Confirmed? 06 Jan 2012</metadata:title> 
<description>Discussing whether the better than expected jobs number point to an economic rebound, and where to invest in this market, with Michael Farr, Farr, Miller, &amp; Washington president.</description> 
<metadata:description xmlns:metadata="http://search.cnbc.com/rss/2.0/modules/siteContentMetadata"><![CDATA[<div class="rss_image" style="float:left;padding-right:10px;"><img border="0" vspace="0" hspace="0" width="93" src="http://thumbnails.cnbc.com/VCPS/Y2012/M01D06/3000066213/6ED1-KR-Jobs_sm.jpg"></div><div class="rss_abstract" style="font:Arial 12px;width:100%;float:left;clear:both">Discussing whether the better than expected jobs number point to an economic rebound, and where to invest in this market, with Michael Farr, Farr, Miller, &amp; Washington president.</div>]]></metadata:description> 
<pubDate>Sat, 07 Jan 2012 00:24 GMT</pubDate> 
<guid isPermaLink="false">http://www.cnbc.com//id/15840232?video=3000066213&amp;play=1</guid> 
<link>http://www.cnbc.com//id/15840232?video=3000066213&amp;play=1</link> 
</item>'; 

$x= new SimpleXMLElement($xmlstr); 
echo $x->title; 
$nss = $x->getNameSpaces(true); 
$metadata = $x->children($nss['metadata']); 
echo $metadata->title, "\n"; 
echo $metadata->description, "\n"; 

Выход

Jobs: Bullish Economy Confirmed?Jobs: Bullish Economy Confirmed? 06 Jan 2012 
<div class="rss_image" style="float:left;padding-right:10px;"><img border="0" vspace="0" hspace="0" width="93" src="http://thumbnails.cnbc.com/VCPS/Y2012/M01D06/3000066213/6ED1-KR-Jobs_sm.jpg"></div><div class="rss_abstract" style="font:Arial 12px;width:100%;float:left;clear:both">Discussing whether the better than expected jobs number point to an economic rebound, and where to invest in this market, with Michael Farr, Farr, Miller, &amp; Washington president.</div> 
+0

Я пытаюсь разобрать этот XML http://www.cnbc.com/id/19838222/device/rss/rss.xml, но получить ошибку, когда я использую SimpleXMLElement. Какие-либо предложения? –

+0

@Sophia Ошибка обычно приходит с полезным сообщением. – kba

+0

Неустранимая ошибка: исключение исключение «Исключение» с сообщением «Строка не может быть проанализирована как XML» –

Смежные вопросы