...

View Full Version : Resolved Parse XML in PHP



Courtney
02-20-2010, 03:15 AM
Hello,

I am a novice at php and xml and am having some difficulty parsing a simple xml document because of what I see as bad structure. I want to parse this link: http://www.wowhead.com/?item=10000&xml

I am using:



$doc = new DOMDocument();
$doc->load('http://www.wowhead.com/?item=10000&xml');
$arrFeeds = array();
foreach ($doc->getElementsByTagName('wowhead') as $node) {
$itemRSS = array (
'item id' => $node->getElementsByTagName('item id')->item(0)->nodeValue,
'name' => $node->getElementsByTagName('name')->item(0)->nodeValue,
'level' => $node->getElementsByTagName('level')->item(0)->nodeValue,
'quality' => $node->getElementsByTagName('quality')->item(0)->nodeValue,
'class id' => $node->getElementsByTagName('class id')->item(0)->nodeValue,
'subclass id' => $node->getElementsByTagName('subclass id')->item(0)->nodeValue,
'inventorySlot id' => $node->getElementsByTagName('inventorySlot id')->item(0)->nodeValue,
'htmlTooltip' => $node->getElementsByTagName('htmlTooltip')->item(0)->nodeValue,
'json' => $node->getElementsByTagName('json')->item(0)->nodeValue,
'jsonEquip' => $node->getElementsByTagName('jsonEquip')->item(0)->nodeValue,
'link' => $node->getElementsByTagName('link')->item(0)->nodeValue,
);
array_push($arrFeeds, $itemRSS);
}
print_r($arrFeeds);


My issue is the tags with the value in the tag like <item id="10000"> my output after parsing has the tag name but no value... [item id] =>

Can someone point me in the right direction?

abduraooft
02-20-2010, 07:35 AM
'item id' => $node->getElementsByTagName('item id')->item(0)->nodeValue, should be
'item id' => $node->getElementsByTagName('item')->item(0)->getAttribute('id'),

Courtney
02-21-2010, 04:55 AM
Thank you!!! I had done so many google searches I was out of ways to form the search terms.

So I learn something from this:

The tag name is actually the first part and the second part is the attribute. And then the actual data inside the tags is the element?

abduraooft
02-21-2010, 06:25 PM
The tag name is actually the first part and the second part is the attribute. And then the actual data inside the tags is the element?
Tag name shouldn't have a blank space and thus what you've said is okay about it. Whatever comes after the tag name before the first > is attribute, and there can be multiple attributes (attribute also has certain rules). An element is the whole thing, consisting of tag name, attributes and the content inside the tags. See http://en.wikipedia.org/wiki/XML#Key_terminology

seco
02-21-2010, 08:43 PM
hey abduraooft, any idea how to parse a xml rowset with php?



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum