tags:

views:

3378

answers:

4

I have an XML file loaded into a DOM document, I wish to iterate through all 'foo' tags, getting values from every tag below it. I know I can get values via

$element = $dom->getElementsByTagName('foo')->item(0);
foreach($element->childNodes as $node){
    $data[$node->nodeName] = $node->nodeValue;
}

However, what I'm trying to do, is from an XML like,

<stuff>
  <foo>
    <bar></bar>
      <value/>
    <pub></pub>
  </foo>
  <foo>
    <bar></bar>
    <pub></pub>
  </foo>
  <foo>
    <bar></bar>
    <pub></pub>
  </foo>
  </stuff>

iterate over every foo tag, and get specific bar or pub, and get values from there. Now, how do I iterate over foo so that I can still access specific child nodes by name?

+2  A: 

Not tested, but what about:

$elements = $dom->getElementsByTagName('foo');
$data = array();
foreach($elements as $node){
    foreach($node->childNodes as $child) {
        $data[] = array($child->nodeName => $child->nodeValue);
    }
}
roryf
No need for $elementCount. Just append to $data like this: $data[] = array($child->nodeName => $child->nodeValue);
troelskn
That did the trick! I was thinking too complicated. Must be tired.
Esa
Updated the code based on comment from troelskn, is that what you meant?
roryf
@Rory: Yes, that's what I meant.
troelskn
+1  A: 

It's generally much better to use XPath to query a document than it is to write code that depends on knowledge of the document's structure. There are two reasons. First, there's a lot less code to test and debug. Second, if the document's structure changes it's a lot easier to change an XPath query than it is to change a bunch of code.

Of course, you have to learn XPath, but (most of) XPath isn't rocket science.

PHP's DOM uses the xpath_eval method to perform XPath queries. It's documented here, and the user notes include some pretty good examples.

Robert Rossney
I'll look into it
Esa
A: 

$data[][$node->nodeName] = $node->nodeValue;

that's another way to do it, if you are lazy

A: 

To iterate through the xml tree we can use the following code at

http://w3mentor.com/learn/php/php-xml/use-php-to-iterate-through-xml-document-tree/