Looking for an item in the XML file in php

0

I have an xml file, and I would like to bring to the page only one item of this file, in case I can bring all the items of one only through the code

<?php
    $feed = file_get_contents('https://brasil.elpais.com/rss/brasil/portada_completo.xml');
    $rss = new SimpleXmlElement($feed);

     //echo "<pre>"; print_r($rss);
    foreach($rss->channel->item as $noticia) {
        echo '<p><a href="'.$noticia->link.'" title="'. $noticia->title .'">'.$noticia->title.''.$noticia->description.'</a></p>';
    }       

?> 

In case I do not want to display all the items, I want only a specific part of the XML, which is in the middle of the generated array, the print of the generated array is this:

In case I want to bring the array [item] [6], only I am not able to do this location, and I would also like to ask some way to integrate xmls, in case I have more than 1 would like to interleave the page tags. If you can help me, I'm grateful.

In the case show the first item tag of the first xml file, then show the first item tag of the second xml file, dps show the fourth item tag of the first xml file, and so on.     

asked by anonymous 11.07.2018 / 20:17

1 answer

0

Simply do this (remember the index starts counting from zero, then 0 is the first item, 1 is the second, etc.):

$rss->channel->item[6];

Example to get the title:

<?php
$feed = file_get_contents('https://brasil.elpais.com/rss/brasil/portada_completo.xml');
$rss = new SimpleXmlElement($feed);

$item_especifico = $rss->channel->item[6];

echo 'Titulo:', $item_especifico->title;

Note that to get @attributes in SimpleXml you should use the ->attributes() method, like this:

<?php
$feed = file_get_contents('https://brasil.elpais.com/rss/brasil/portada_completo.xml');
$rss = new SimpleXmlElement($feed);

$item_especifico = $rss->channel->item[6];
$enclosure = $item_especifico->enclosure->attributes();

echo 'Titulo:', $item_especifico->title, '<br>';
echo 'Imagem:', $enclosure->url, '<br>';
echo 'Peso da imagem:', $enclosure->length, '<br>';
    
11.07.2018 / 20:21