Read XML with equal PHP nodes

Asked

Viewed 229 times

1

I have the xml example:

<Listings>
    <Listing>
        <Title>Nome</Title>
        <Details>
            <Description>
                dados
            </Description>
        </Details>
        <Media>
            <Item medium="image">
                https://site.com.br/PM.jpeg
            </Item>
            <Item medium="image">
                https://site.com.br/PM.jpeg
            </Item>
            <Item medium="image">
                https://site.com.br/PM.jpeg
            </Item>
        </Media>
    </Listing>
        <Listing>
            <Title>Nome</Title>
            <Details>
                <Description>
                    dados
                </Description>
            </Details>
            <Media>
                <Item medium="image">
                    https://site.com.br/PM.jpeg
                </Item>
                <Item medium="image">
                    https://site.com.br/PM.jpeg
                </Item>
                <Item medium="image">
                    https://site.com.br/PM.jpeg
                </Item>
            </Media>
        </Listing>
</Listings>

I’m reading with the function simplexml_load_file, data reading occurs normally, my problem is how to read all items within the node <Media> I tried it this way:

foreach($xml->Listings as $registro):
     echo 'Título: ' . $registro->Listing->Title . '<br>';

            foreach($registro->Listing->Media as $item):
                echo 'Imagem: ' . $item->Item . '<br>';
            endforeach;

endforeach;
  • 1

    Dear, it’s okay that you chose an answer "try this", which does not explain anything, is your choice and I cannot and will not attempt to interfere... yet I have added an extra explanation about the use of Trim to remove the unnecessary extra spaces: https://answall.com/a/326045/3635. which may be useful to you.

2 answers

3


The first element (called root element) is the <Listings>, that is, the object in $xml already is the <Listings>, so there’s no point in trying to catch $xml->Listings, for the $xml is already him.

Also missing iterate the <Items> within the <Media>, since it’s more than one, then you have to use a loop, like foreach

The right thing would be:

$listings = simplexml_load_string($xml);

foreach ($listings as $listing):

    echo 'Título: ' . trim($listing->Title) . "<br>";

    foreach($listing->Media as $items):
        foreach($items as $item):
            echo 'Imagem: ' . trim($item) . "<br>";
        endforeach;
    endforeach;

endforeach;

Note that your xml returns spaces between urls, so with trim() you can eliminate unnecessary spacing.

1

Good night,

Try it here:

foreach($xml->Listings as $registro):
     echo 'Título: ' . $registro->Listing->Title . '<br>';

            foreach($registro->Listing->Media as $item):
            $loop = count($item);    

            for($i = 0; $i < $loop; $i++){
                    echo 'Imagem: ' . $item->Item[$i] . '<br>';
            }

            endforeach;

endforeach;

Browser other questions tagged

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