Page 1 of 1

parsing xml via dom - node child reference problem

Posted: Sun Jul 01, 2007 12:12 pm
by j0hn
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


I can reference a XML tag with the code below but don't understand how I can reference it's child nodes (or tags)

Code: Select all

$dom = new DomDocument;
$dom->load("book.xml");
$names = $dom->getElementsByTagName("competition");

foreach ($names as $name) {
       $arrayOfPlayerNames[$i] = $name -> nodeValue.'<br />';
	echo 'name '.$i.' is: '.$arrayOfPlayerNames[$i];
	$i++;
}
Code above displays values for all <competition> tags


I want to know how I can see the childldren of <competition>.

I'm trying to parse selected parts of the xml file to mysql.

Many thanks


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Sun Jul 01, 2007 5:03 pm
by superdezign
Have you read the documentation?

Posted: Sun Jul 01, 2007 7:42 pm
by j0hn
I have read the documentation extensively and tried many different things but I can't seem to get anything to work.

I'm pretty sure that I have read the information I need already, I just don't understand it. A working example would do wonders for me.

Many thanks

Posted: Sun Jul 01, 2007 8:05 pm
by superdezign
Working example? Naw, too easy.

Have you read any of the DOMNode documentation?

getElementsByTagName gives you a DOMNodeList. The DOMNodeList has a length and an item() function to access it's DOMNodes.

If you somehow missed all of that, you didn't read it "extensively." I've never even used it before, and I already see this much. It's even got examples in the documentation. Read it, read it, read it. :D

Posted: Sun Jul 01, 2007 8:11 pm
by alex.barylski
superdezign wrote:Working example? Naw, too easy.

Have you read any of the DOMNode documentation?

getElementsByTagName gives you a DOMNodeList. The DOMNodeList has a length and an item() function to access it's DOMNodes.

If you somehow missed all of that, you didn't read it "extensively." I've never even used it before, and I already see this much. It's even got examples in the documentation. Read it, read it, read it. :D
The docs are actually not that great. I just spent a couple days trying to figure the damn thing out and that's with experience in manipulating the DOM in JavaScript. So I can concur with the OP. A working example would work wonders.

I don't yet need to iterate a list of children tags using DomDocument but I will shortly.

OP: In the meantime, if all you are trying to do is iterate the list of children and use their data, you might as well use SimpleXML as it's much easier than DocumentDom.

Posted: Sun Jul 01, 2007 9:21 pm
by superdezign
Like I said, the docs have examples in them, and I believe the one on DOMNodeList does exactly what he's after.

Posted: Mon Jul 02, 2007 2:36 am
by j0hn
Many thanks for your replies.

I have read the docs here http://us2.php.net/dom extensively ( or at least 5 times) but with my limited knowledge of php I don't understand them properly. I think my lack of php objects knowledge is letting me down. Perhaps with proper knowledge the docs aren't as ambigious.

I want to learn and I can understand why a working examle is 'too easy' but I can't learn the correct syntax without ever seeing it. I think the key is understanding '->', I did google it but nothing comes up. Can anyone recommend any pages on php objects?

I will look at SimpleXML as I only need to read XML documents



I'm at work so can't test this code right now, I think it should display the values of the child nodes.

Code: Select all

$dom = new DomDocument;
$dom->load("book.xml");
$names = $dom->getElementsByTagName("competition");

foreach ($names as $name) {

       echo $name->nodeValue.'<br />'; // displays value of current instance of <competition>?
       echo $name->length.'<br />'; // displays no of child nodes for the current instance of <competition>?

       for ($i = 0; $i <= $name -> length; $i++){ 
            $thisChild = $name -> item($i);
            echo $thisChild -> nodeValue.'<br>'; // displays the value of the current child node of <competition>?
       }
}

Posted: Mon Jul 02, 2007 6:02 am
by superdezign
j0hn wrote:I have read the docs here http://us2.php.net/dom extensively ( or at least 5 times) but with my limited knowledge of php I don't understand them properly. I think my lack of php objects knowledge is letting me down. Perhaps with proper knowledge the docs aren't as ambigious.
Yeah, this is very object-oriented, but after you get used to it, you'll likely prefer it this way.

Anyway, in that code, you're not quite doing it right yet. DOMNode object's don't have a length property, DOMNodeList objects do (but you've eliminated the need for it by using foreach). The child nodes are a linked list. You get them by looping through the list.

Code: Select all

for($current = $name->firstChild; $current->nextSibling != NULL; $current = $current->nextSibling)
And if you ever wanted to go backwards, you'd start at lastChild and work your way through previousSiblings.

And the item() function is, also, only for DOMNodeList. But, again, you've eliminated the need for any of DOMNodeList's functions by using the foreach loop.

Posted: Tue Jul 03, 2007 1:41 am
by j0hn

Code: Select all

for($current = $name->firstChild; $current->nextSibling != NULL; $current = $current->nextSibling)
Wow, that is very concise. I like it, the simplicity is beautiful, I think I will prefer the object-oriented approach.

It's clicking into place now, thanks for the tips.