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?
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.
– Rafael
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.– MagicHat
I made an issue see if now goes...
– MagicHat
I did what you said, then I used print_r($xpath) and showed the following: Domxpath Object ( [Document] => (Object value omitted) )
– Rafael
Look at the edition I made, notice there that I changed :
$dom->loadXML($xml);
for$dom->load($xml);
.– MagicHat
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?
– Rafael
Yes, I’ll edit it... Then you use the
for
...Ready see now– MagicHat
Oh I did one more edit, I’m doing something else here, I’m a little inattentive... Now it’s round...
– MagicHat
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!!!!!!
– Rafael