How to get images in XML CDATA and DESCRIPTION

Asked

Viewed 303 times

1

I have some xml files, and several of them the news images are inside the Description, in one of them the image is inside the CDATA, at the time I return the array to Description does not appear any element according to the photo:

array gerado pelo SimpleXmlElement

in another case the image is already inserted inside the Description that does not contain a CDATA as in the image below:

No caso a imagem ai é a onça

we two cases I’m not able to find a way to manipulate these images the way I want, I was wondering if you could help me,

I’ll post my code here too, for now it’s simple I’m still developing it, and I can’t get out of that part.

 $feedwwf = file_get_contents('https://www.wwf.org.br/rss/rss.cfm?B5ABCD22- 
A9C2-3BB4-44189EF9064E5481',LIBXML_NOCDATA);    
 $rsswwf = new SimpleXMLElement($feedwwf);
  $wwf = $rsswwf->channel->item[0];
  $desc = $wwf->description;


$feedg1 = file_get_contents('http://pox.globo.com/rss/g1/');
$rssg1 = new SimpleXMLElement($feedg1);
$g1 = $rssg1->channel->item[0];
$desc = $g1->description->attributes('img') ;

1 answer

2


PHP Offers a very comprehensive class for manipulating Domdocument XML

[ http://php.net/manual/en/class.domdocument.php ]

You can manipulate the document as follows:

$dom = new DOMDocument();
$dom->load('http://pox.globo.com/rss/g1/');

# localizando um elemento dentro do xml
$url = $dom->getElementsByTagName('url');

# alterando o primeiro item ou seja o primeiro elemento
$url->item(0)->nodeValue = 'imagem_teste.png';

$dom->saveXML();

Browser other questions tagged

You are not signed in. Login or sign up in order to post.