Reputation: 9
Sorry for spamming. But I couldn't find a answer for my question.
I have a XML and a element of it is inside of a CDATA, and I can't get it.
It's:
<description><![CDATA[<img src='http://w3.i.uol.com.br/Wap/2010/01/19/midia-indoor-desemprego-seguro-desemprego-carteira-de-trabalho-1263914866285_142x100.jpg' align="left" /> Trabalhadores do Rio de Janeiro que buscam o seguro-desemprego têm enfrentado filas que começam na noite anterior ao dia do atendimento. Hoje (17), na agência do Poupa Tempo da Central do Brasil, no centro da capital fluminense, cerca de duzentas pessoas já aguardavam na fila às 8h, quando o atendimento começou. ]]></description>
So.. as you can see, the tag is inside of CDATA, when I try get it, is showed me a blank screen.
I was trying something like:
$xml = simplexml_load_file('http://rss.uol.com.br/feed/noticias.xml', 'SimpleXMLElement',LIBXML_NOCDATA);
echo $xml->channel->item[2]->description->img['src'];
Please, I tried for a few hours. If anyone can help me, I appreciate it.
Sorry my english.
Upvotes: 0
Views: 838
Reputation: 163595
CDATA means "character data". It means "even if the stuff in here looks like markup, treat it like text". So there are no tags or elements in CDATA, only character strings that to a human reader might resemble tags or elements.
To look at it another way, CDATA tells the parser not to process the content.
So if you do want to process the content, you either have to get rid of the CDATA tags, or you have to put the content through a second stage of parsing: it wasn't parsed the first time around, so you need to take the text that's inside the CDATA section, and feed it back through another stage of parsing.
Upvotes: 1
Reputation: 97331
Since the content in your CDATA is actually HTML, you're probably better off parsing it as HTML and getting to its contents that way:
<?php
$descriptionXml = "<description><![CDATA[<img src='http://w3.i.uol.com.br/Wap/2010/01/19/midia-indoor-desemprego-seguro-desemprego-carteira-de-trabalho-1263914866285_142x100.jpg' align='left' />Trabalhadores do Rio de Janeiro que buscam o seguro-desemprego têm enfrentado filas que começam na noite anterior ao dia do atendimento. Hoje (17), na agência do Poupa Tempo da Central do Brasil, no centro da capital fluminense, cerca de duzentas pessoas já aguardavam na fila às 8h, quando o atendimento começou. ]]></description>";
$description = simplexml_load_string($descriptionXml);
$dom = new DOMDocument();
$dom->loadHTML($description);
echo $dom->getElementsByTagName('img')->item(0)->getAttribute('src');
?>
Upvotes: 0