Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-20-2010, 03:15 AM   PM User | #1
Courtney
New Coder

 
Join Date: Feb 2010
Posts: 20
Thanks: 2
Thanked 2 Times in 2 Posts
Courtney is an unknown quantity at this point
Parse XML in PHP

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:

Code:
$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?

Last edited by Courtney; 02-21-2010 at 06:31 AM..
Courtney is offline   Reply With Quote
Old 02-20-2010, 07:35 AM   PM User | #2
abduraooft
Supreme Master coder!

 
abduraooft's Avatar
 
Join Date: Mar 2007
Location: N/A
Posts: 14,680
Thanks: 158
Thanked 2,182 Times in 2,169 Posts
abduraooft is just really niceabduraooft is just really niceabduraooft is just really niceabduraooft is just really niceabduraooft is just really nice
Quote:
'item id' => $node->getElementsByTagName('item id')->item(0)->nodeValue,
should be
Code:
'item id' => $node->getElementsByTagName('item')->item(0)->getAttribute('id'),
__________________
Quote:
The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)
abduraooft is offline   Reply With Quote
Users who have thanked abduraooft for this post:
Courtney (02-21-2010)
Old 02-21-2010, 04:55 AM   PM User | #3
Courtney
New Coder

 
Join Date: Feb 2010
Posts: 20
Thanks: 2
Thanked 2 Times in 2 Posts
Courtney is an unknown quantity at this point
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?
Courtney is offline   Reply With Quote
Old 02-21-2010, 06:25 PM   PM User | #4
abduraooft
Supreme Master coder!

 
abduraooft's Avatar
 
Join Date: Mar 2007
Location: N/A
Posts: 14,680
Thanks: 158
Thanked 2,182 Times in 2,169 Posts
abduraooft is just really niceabduraooft is just really niceabduraooft is just really niceabduraooft is just really niceabduraooft is just really nice
Quote:
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
__________________
Quote:
The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)
abduraooft is offline   Reply With Quote
Old 02-21-2010, 08:43 PM   PM User | #5
seco
Regular Coder

 
seco's Avatar
 
Join Date: Nov 2008
Location: Oregon
Posts: 682
Thanks: 5
Thanked 79 Times in 77 Posts
seco has a little shameless behaviour in the past
hey abduraooft, any idea how to parse a xml rowset with php?
seco is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 04:10 AM.


Advertisement
Log in to turn off these ads.