Web Analytics Made Easy -
StatCounter Reading XML with Namespace - Can't get Child Node - CodingForum

Announcement

Collapse
No announcement yet.

Reading XML with Namespace - Can't get Child Node

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Reading XML with Namespace - Can't get Child Node

    Hi Guys

    I'm having a bit of bother with getting at a name space node. In fact, I've now wasted the better part of a day trying to get at it...

    I have an XML file that I'm pulling down and I can access most of the nodes fine, even the name space nodes are ok. My problem is that I can't access a "child" namespace node. I think I'm pretty close and I'm just missing out on the semantics. But then again, I have been massively wrong before

    Here is a bit of the XML:

    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <feed xmlns:ls="http://yourcompany.com/ns/1.0" xml:lang="en-US" xmlns:georss="http://www.georss.org/georss" xmlns="http://www.w3.org/2005/Atom">
      <id>tag:livingsocial.com,2005:/cities</id>
      <link type="text/html" href="http://yourcompany.com" rel="alternate"/>
      <title>The Title</title>
      <updated>2011-08-25T08:28:55Z</updated>
      <entry>
        <id>The ID</id>
        <title>The Title</title>
        <content type="html">The HTML Content</content>
        <ls:merchant>The Merchant Name</ls:merchant>
        <ls:location>
          <ls:address1>Adress</ls:address1>
          <ls:city>The City</ls:city>
          <ls:state>The State</ls:state>
          <ls:zip>The Zip Code</ls:zip>
          <ls:country>The Country</ls:country>
          <ls:phone>The Phone Number</ls:phone>
          <georss:point>-28.0067947 153.4065773</georss:point>
          <georss:featureTypeTag>location</georss:featureTypeTag>
          <ls:latitude>lat</ls:latitude>
          <ls:longitude>lat</ls:longitude>
        </ls:location>
        <author>
          <name>Author Name</name>
        </author>
      </entry>
    and here is a bit of the boiled down parser:

    Code:
    // load the file on the DOM
    $dom = new DomDocument();
    $dom->load('http://yourcompany.com/xml');
     
    $itemList 		= array();
     
    // get the list of Items.
    $itemElemList 	= $dom->getElementsByTagName('entry');
    for($i=0;$i<$itemElemList->length;$i++) {
    	$itemList[$i] = array (
    		'title'       	 => $itemElemList->item($i)->getElementsByTagName('title')->item(0)->nodeValue,
    		'link'           => $itemElemList->item($i)->getElementsByTagName('link')->item(0)->nodeValue,
    		'description' => $itemElemList->item($i)->getElementsByTagName('description')->item(0)->nodeValue,			
    		'city'           => $itemElemList->item($i)->getElementsByTagNameNS('http://yourcompany.com/ns/1.0', 'city')->item(0)->nodeValue
    	);
    
     
    }
    Basically, I am trying to get the value from the ls:city node but this is my first real stab at namespaces and I don't really know how to get in at the child node of ls:location.

    Any help would be massively appreciated.
    Last edited by woodpecker63; Aug 25, 2011, 09:06 AM.

  • #2
    Ahh... False alarm... I was using the wrong namespace e.g. I was using:

    Code:
    getElementsByTagNameNS('http://www.w3.org/2005/Atom', 'city')
    instead of:

    Code:
    getElementsByTagNameNS('http://yourcompany.com/ns/1.0', 'city')
    It's always the small things...

    Comment


    • #3
      An easer way to get the text node if you can't access to [0] property of the simpleXMLElement is to use the sprintf function.

      As gwhitescarver at yahoo dot com said, in some version of php 5 it is hard to access to text node of an element, but when you print the element, the [0] property is automaticaly returned.

      Like if you echo() the element, the text node is return.

      The sprintf function do the same:

      <?php
      $a = array();

      foreach ($xml->children() as $child) {
      $a[$child->getName()] = sprintf("%s", $child);
      }
      Showcase your talents online

      Comment

      Working...
      X