Treating XML file with PHP

Asked

Viewed 1,280 times

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 showing only the title, without the link. It shows the value, without the splitting. It does not show the thumbnail.

Adding ----->

According to the code above, it brings a blank result always in the first line.

2 answers

3


Well, according to your XML, you are working with an offer, so when you use foreach to traverse XML, it will go through all the parent nodes of the main tag. So when you make the call, for example, "$Details->offerName", you will get a blank.

I advise you to read and call the elements individually 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 >';
?>
  • tried so, and no data appeared on the screen.

  • Wrong, he is iterating offer. Para acessar dessa forma seria algo como $Details->Offer->blablablabla`

  • It is not, it is iterating the Offer children nodes check in the PHP manual link the examples.

  • I think it’s around @casscarraro , problem is that not all data is appearing, I’ve tested in several ways and nothing.

  • Actually, it’s iterating the kids. I ran the code here. My fault.

  • Well, @Gustavocave to get the elements you can use as in the example I quoted above, use the $Details->"node name". If you want to search all the data you can use the foreach, however you should take care of the elements that have children, as in the case of "links", you will have to use another foreach to browse or search for a fixed element as I gave the example of "$Details->links[0]".

  • @casscarraro created 1 variable $a = links[0]->link["url"] and called the variable in echo inside href. I haven’t had any success yet.

  • You should assign the value of $Details->links[0]->link["url"], ie $a = $Details->links[0]->link["url"].

  • Infeslimente without success, I will try other ways, any other help is welcome, thanks for everything so far. casscarraro and gmsantos!

  • Inside the xml in the <link> tag has a call type type=Offer, this is not influencing will?

  • I made an echo $Details->links[0]->link["url"] . <br>; and showed the link, but when I put it inside href, it doesn’t work

  • It worked, but without using variable. It looked like this: echo "<a href='" . $Details->links[0]->link["url"] ." '>" . $Details->offerName . " </a><br >";

Show 7 more comments

1

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

Note also that some data is element attributes. You can access these attributes through an element array.

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


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

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

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

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

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

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

4 - The link is not appearing because the link tag is incorrect, the 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 >";

?>
  • Only the installment worked. The link and the thumbnail not yet.

  • Knows any solution?

  • Fatal error: Call to a Member Function Attributes() on a non-object...

  • Now yes. Corrected

  • Without success, nothing appears on the screen. I tried my original code, adding the variables $link_url and $imagem_url and calling them in echo and did not succeed tbm, but the data appears.

Browser other questions tagged

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