Interpret XML file tags with PHP

Asked

Viewed 355 times

3

I’m having trouble interpreting an XML file with PHP. The difficulty I have is not being able to get the values according to their tag. The xml loop brings this difficulty to me. See a summary of it:

<?xml version="1.0"?>
<root>
    <id_couponoffer>
        <value>
            <![CDATA[192756]]>
        </value>
    </id_couponoffer>
    <title>
        <value>
            <![CDATA[Fast Runner]]>
        </value>
    </title>
    <startdate>
        <value>
            <![CDATA[2016-11-30]]>
        </value>
    </startdate>
    <enddate>
        <value>
            <![CDATA[2017-03-31]]>
        </value>
    </enddate>
    <url>
        <value>
            <![CDATA[http://v2.afilio.com.br/tracker.php?banid=4295676&campid=26275;2043&siteid=32790]]>
        </value>
    </url>
    <discount>
        <value>
            <![CDATA[0]]>
        </value>
    </discount>
    <progid>
        <value>
            <![CDATA[2043]]>
        </value>
    </progid>
    <rule>
        <value>
            <![CDATA[Oferta]]>
        </value>
    </rule><code><value><![CDATA[sem]]></value></code>
    <description>
        <value>
            <![CDATA[Lançamento: TÊNIS HOKA M BONDI 4 Por R$ 899,90]]>
        </value>
    </description>
    <type>
        <value>
            <![CDATA[n]]>
        </value>
    </type>
</root>

I’m not able to use foreach to pick up just a few values.

$xml = simplexml_load_file('http://v2.afilio.com.br/api/feedproducts.php?token=53e355fe881a30.10592737&mode=dl&siteid=32790&affid=26275&format=XML');

foreach ($xml as $result) {

    echo $result->value;
    echo '</br>';
}

When I use $result->value, it pulls all the xml values. What I want is to take just a few and organize them.

How can I do?

1 answer

2


You can use Xpath for that :

<?php
$dom = new DOMDocument();
$xml = "data_export.xml";
$dom->load($xml); 
$xpath = new DOMXPath($dom);
$items = $xpath->query('/root/id_couponoffer');
for($i = 0; $i < $items->length; $i++)
{
    $id_couponoffer = $xpath->query('/root/id_couponoffer');
    echo "id_couponoffer    :".$id_couponoffer->item($i)->nodeValue."<br/>";

    $title = $xpath->query('/root/title');
    echo "title :".$title->item($i)->nodeValue."<br/>";

    $startdate = $xpath->query('/root/startdate');
    echo "startdate :".$startdate->item($i)->nodeValue."<br/>";
}
echo "E assim por diante....";
?>

Take a look at this question(note that it uses html and not xml) and see other ways to implement.

  • So, the code with the xml I just posted to show the tags, but I can’t see it that way in PHP. In php, I upload the XML file, as seen before foreach. Thanks for the help.

  • Just put the file here : $dom->loadXML("arquivo.xml");, remember if any answer is correct consider validating it by clicking on the icon below the evaluation arrows.

  • I made an issue see if now goes...

  • I did what you said, then I used print_r($xpath) and showed the following: Domxpath Object ( [Document] => (Object value omitted) )

  • Look at the edition I made, notice there that I changed : $dom->loadXML($xml); for $dom->load($xml);.

  • Yes, after your editing went well, it showed the results. In this case, foreach refer to a specific element, generating all id_couponoffer first, then the title sequence and so on. I want to organize each id_couponoffer with its respective title, it is possible with Xpath?

  • Yes, I’ll edit it... Then you use the for...Ready see now

  • Oh I did one more edit, I’m doing something else here, I’m a little inattentive... Now it’s round...

  • Magichat, thank you very much. At the same time that you were answering I tried to find a light here, even trying, unsuccessfully, to use the for. After that your last edition was perfect. Thank you so much for your help. Thanks so much!!!!!!

Show 4 more comments

Browser other questions tagged

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