Handling XML file with PHP

0

I have the following XML:

<offer categoryId="3588" id="154394195459775522">
    <offerName>
        CAMISETA DROP DEAD PRODUCTION – MASCULINA VERDE ESCURO
    </offerName>
    <links>
        <link url="http://links.lomadee.com/ls/d25zZjsyRHd2U1I3MzsyODY4NjE3OTswOzU3OTg7MDs1NzE0O0JSOzM7aHR0cCUzQSUyRiUyRnd3dy5jZW50YXVyby5jb20uYnIlMkZwcm9tb2NhbyUzRm5leHRVcmwlM0R0LXNoaXJ0LWRyb3AtZGVhZC1wcm9kdWN0aW9uLTcwMjE0MDE4LTgxODE2OC5odG1sJTI2dHlwZSUzRHBkcCUyNmlkJTNEODE4MTY4JTI2dXRtX3NvdXJjZSUzRExvbWFkZWUlMjZ1dG1fbWVkaXVtJTNEeG1sJTI2dXRtX2NhbXBhaWduJTNETG9tYWRlZS1Ta2F0ZS1DYW1pc2V0YS0tODE4MTY4LTg2JTI2b3JpZ2VtJTNETG9tYWRlZTswOzA-.html" type="offer"/>
    </links>
    <thumbnail url="http://images.centauro.com.br/250x250/81816886.jpg"/>
    <price>
        <currency abbreviation="BRL"/>
        <value>49.9</value>
        <parcel>
            <value>24.95</value>
            <number>2</number>
            <interest>0.0</interest>
        </parcel>
    </price>
    <seller id="84" isTrustedStore="false" pagamentoDigital="false" advertiserId="5714" oneClickBuy="false" oneClickBuyValue="0" cpcDifferentiated="false">
        <sellerName>Centauro</sellerName>
        <thumbnail url="https://wwws.lomadee.com/programas/BR/5714/logo_185x140.png"/>
        <links>
            <link url="http://www.centauro.com.br" type="seller"/>
        </links>
    </seller>
    <startOffer>1969-12-31T21:00:00-03:00</startOffer>
    <endOffer>8994-08-17T04:12:55.807-03:00</endOffer>
    <category id="3588" isFinal="true">
        <thumbnail url="http://imagem.buscape.com.br/bp5/categorias/3588.jpg"/>
        <name>Shorts Esportivo</name>
    </category>
</offer>

I called xml, via php as follows:

 <?php 
$xml = simplexml_load_file('arquivo.xml');  
foreach ($xml as $details){
    echo "<href='" . $details->links ."'>" . $details->offerName . "</a><br >";
    echo $details->thumbnail . '<br >';
    echo "R$" . $details->price->value . '<br >';
    echo $details->price->parcel->value->number . '<br >';
    echo $details->seller->sellerName . '<br >';

    echo '<br >';
    }
?>

The problem is that it is only showing the title, without the link. Shows the value, without the installment. Do not show the thumbnail.

Adding ----- >

Depending on the code you entered above, it always brings a blank result to the first line.

    
asked by anonymous 21.09.2014 / 18:46

2 answers

3
Well, according to your XML, you're working with an offer, so when you use foreach to traverse the XML, it goes through all the child nodes of the parent tag. So when you make the call, for example, "$ details-> offerName", it will return an empty.

I advise you to read and call the elements individually in this way:

<?php 
    $details = simplexml_load_file('arquivo.xml');  

    echo "<a href='" . $details->links[0]->link["url"] ."'>" . $details->offerName . "</a><br >";
    echo $details->thumbnail["url"] . '<br >';
    echo "R$" . $details->price->value . '<br >';
    echo $details->price->parcel->value->number . '<br >';
    echo $details->seller->sellerName . '<br >';
    echo '<br >';
?>
    
21.09.2014 / 19:20
1

Pay attention when using XML. All of your errors are related to trying to use elements in the wrong way, without following the structure of your document.

Note also that some data are attributes of the elements. You can access these attributes through an array of the element.

As also observed in the @casscarraro response, its foreach is iterating the children of offer . Since your file only has one offer, foreach is unnecessary.

To display the thumbnail, you must place the content inside the img tag and access the url attribute.

echo '<img src="' . $details->thumbnail["url"] . '">';

2 - $details->links is a group of links, the correct one is to associate with the correct element

echo "<href='" . $details->links->link->["url"] ."'>"

3 - Again, you specified the wrong path in your xml to display the parcel.

echo $details->price->parcel->number . '<br >';

4 - The link is not showing because the link tag is incorrect, a

echo "<a href='{$link_url}'> {$xml->offerName} </a><br >";

At the end the corrected code looks like this:

<?php

$xml = simplexml_load_file('arquivo.xml');

$link_url = $xml->links->link["url"];
$imagem_url = $xml->thumbnail["url"];

echo "<a href='{$link_url}'> {$xml->offerName} </a><br >";
echo "<img src='{$imagem_url}'><br >";
echo "R$ {$xml->price->value} <br >";
echo "{$xml->price->parcel->number}x de {$xml->price->parcel->value} <br >";
echo "{$xml->seller->sellerName} <br >";

echo "<br >";

?>
    
21.09.2014 / 19:05