Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Feb 2010
    Posts
    20
    Thanks
    2
    Thanked 2 Times in 2 Posts

    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 07:31 AM.

  • #2
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,866
    Thanks
    160
    Thanked 2,224 Times in 2,211 Posts
    'item id' => $node->getElementsByTagName('item id')->item(0)->nodeValue,
    should be
    Code:
    'item id' => $node->getElementsByTagName('item')->item(0)->getAttribute('id'),
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • Users who have thanked abduraooft for this post:

    Courtney (02-21-2010)

  • #3
    New Coder
    Join Date
    Feb 2010
    Posts
    20
    Thanks
    2
    Thanked 2 Times in 2 Posts
    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?

  • #4
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,866
    Thanks
    160
    Thanked 2,224 Times in 2,211 Posts
    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
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #5
    Regular Coder seco's Avatar
    Join Date
    Nov 2008
    Location
    Oregon
    Posts
    687
    Thanks
    6
    Thanked 79 Times in 77 Posts
    hey abduraooft, any idea how to parse a xml rowset with php?


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •