I am running into the issue that some nodes of my xml file are parsed & displayed correctly while others aren't detected (at least I don't know what's going wrong here)
Rather than posting the xml file I will provide a link to it. Here's a small XML snippet for you to review the xml structure:
<offers version="1"><group name="games">
<o id="1" url="http://inexus.us/world-of-warcraft-eu/pre-paid-game-time-card-60-days" price="21.53" avail="1">
<name>World of Warcraft EU Pre-Paid Game Time Card 60 Days</name>
<currency>
EUR
</currency>
</o>
Now, I am using this code to parse/read the xml file.
$xmlDOM = new DOMDocument();
$xmlDOM->load("http://inexus.us/compare.xml");
$document = $xmlDOM->documentElement;
foreach ($document->childNodes as $node) {
if ($node->hasChildNodes()) {
foreach($node->childNodes as $temp) {
echo $temp->nodeName."=".$temp->nodeValue."<br />";
}
}
}
Using that code I am getting the name of each elemet o
However I also need to get the information stored inside the o element... (i.e. id, url, price) but I don't quite understand how I can access them.
Also the output returns several #text= blocks. (I guess this happens because of whitespaces in the xml?)
A small snippet of the Output:
#text=
#text=
o= World of Warcraft EU Pre-Paid Game Time Card 60 Days EUR
#text=
o= World of Warcraft EU Battle Chest cd-key EUR
#text=
o= World of Warcraft EU Cataclysm cd-key EUR
#text=
Any help/hint is appreciated!
The #text has to deal with the whitespace. You can use preserveWhiteSpace = false (see below), but you must remember to use this before you load().
As for the attributes, you can use the hasAttributes() to check the node has attributes then iterate through the node's attributes using attributes.
In the example below i took a shortcut and grabbed all of the o tags:
<?php
$xmlDOM = new DOMDocument();
$xmlDOM->preserveWhiteSpace = false;
$xmlDOM->load("http://inexus.us/compare.xml");
$offers = $xmlDOM->getElementsByTagName('o');
foreach ($offers as $offer) {
if($offer->hasAttributes()){
foreach($offer->attributes as $attr){
$name = $attr->nodeName;
$value = $attr->nodeValue;
echo $name.' = '.$value.'<br>';
}
}
if ($offer->hasChildNodes()) {
foreach($offer->childNodes as $o) {
echo $o->nodeName."=".$o->nodeValue."<br />";
}
}
echo '<hr>';
}?>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With