Looking for an item in the php XML file

Asked

Viewed 145 times

0

I have an xml file, and I would like to bring to the page only one item of that file, in case I can bring all the items of a view 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 don’t want to present all the items, I just want a specific part of XML, which is in the middle of the generated array, the print of the generated array is this: array

in case I want to bring the [item][6] array, 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 I would like to insert the selected tags on the page. If you can help me I’m grateful.

In 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.

1 answer

0


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

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

Example to catch 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 catch @attributes Simplexml must use the method ->attributes(), thus:

<?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>';
  • Great super worked, I wanted to ask a question, inside the tag item, there is a tag [Enclosure] which is an array with 2 attributes like this: [Enclosure] => Array ( [0] => Simplexmlelement Object ( [@Attributes] => Array ( [url] => https://ep00.epimg.net/politica/imagenes/2018/07/11/actualidad/1531322308_202368_1531325055_miniatura_normal.jpg [length] => 26887 [type] => image/jpeg ) [1] => Simplexmlelement Object... ai with the same elements as above, only changes the [url] tag. and wanted to display the [Enclosure] tag’s Dice 1 and it shows me nothing.

  • @Brunoclementin edited the answer and put an example of how to take the attributes of Enclosure

  • too @Guilhermenascimento, I’m new to the programming world and you’ve been of great help, I’m extremely grateful for your help. , just one more thing, in case it shows me the http of the url, and not the image itself, know how to solve to show the image ?

  • @Brunoclementin like this echo '<img src="', $enclosure->url,'">';

  • Sensational thank you very much!

Browser other questions tagged

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