By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,614 Members | 1,680 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,614 IT Pros & Developers. It's quick & easy.

DOM child finding

P: n/a
Is there any faster/easier way of finding a child node of a certain
DOMNode object than doing something like

function getChildByTagname($curnode, $name)
{
foreach($curnode->childNodes as $node)
{
if($node->nodeName==$name)
{
return $node;
}
}
}

?

it seems fairly slow and cumbersome. i was thinking xpath, but i can
hardly imagine that to be a lot faster, with all the DOMXpath object
creation and all, though I didn't benchmark.

-egbert
Apr 24 '07 #1
Share this Question
Share on Google+
2 Replies


P: n/a

"skrebbel" <e.**************@student.apple.tue.banana.nlwro te in message
news:op***************@s011169.mshome.net...
Is there any faster/easier way of finding a child node of a certain
DOMNode object than doing something like

function getChildByTagname($curnode, $name)
{
foreach($curnode->childNodes as $node)
{
if($node->nodeName==$name)
{
return $node;
}
}
}

?

it seems fairly slow and cumbersome. i was thinking xpath, but i can
hardly imagine that to be a lot faster, with all the DOMXpath object
creation and all, though I didn't benchmark.

-egbert


I would have thought that you could probably narrow it down with an XPATH
query, but without seeing the XML and what you're after, it's a little hard
to consider what you are looking for.

Apr 24 '07 #2

P: n/a
On Tue, 24 Apr 2007 11:55:27 +0200, Sean
<sean.anderson@[nospam]oakleafgroup.bizwrote:
>
"skrebbel" <e.**************@student.apple.tue.banana.nlwro te in
message
news:op***************@s011169.mshome.net...
Is there any faster/easier way of finding a child node of a certain
DOMNode object than doing something like

function getChildByTagname($curnode, $name)
{
foreach($curnode->childNodes as $node)
{
if($node->nodeName==$name)
{
return $node;
}
}
}

?

it seems fairly slow and cumbersome. i was thinking xpath, but i can
hardly imagine that to be a lot faster, with all the DOMXpath object
creation and all, though I didn't benchmark.

-egbert


I would have thought that you could probably narrow it down with an XPATH
query, but without seeing the XML and what you're after, it's a little
hard
to consider what you are looking for.
What I'm after is a method that gets a child node of a certain "current
node" that has a certain name. It is for a general purpose solution I'm
making.

As an example, in

<books>
<book>
<title>Bananas, and how to peel them</title>
<author>John Doe</author>
<review>
<author>Perry White</author>
<title>New banana peeling book revolutionalises world of banana
peeling</title>
</review>
</book>
</books>

if i have a DOMNode object $book that refers to the above book node, i'd
want the fastest way to get me a DOMNode object that refers to its title
child (but of course not to the review's title, which is what
$document->getElementsByTagName would give me).

With SimpleXML, I could of course just do $doc->books->book->title, but I
don't want to use SimpleXML because it is so awfully terrible at anything
other than reading out some data (try deleting a node or adding features,
for instance). So, what's the fastest/best/etc way to do it with DOM?

-egbert
Apr 24 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.