DOM child finding

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • skrebbel

    DOM child finding

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

    function getChildByTagna me($curnode, $name)
    {
    foreach($curnod e->childNodes as $node)
    {
    if($node->nodeName==$nam e)
    {
    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
  • Sean

    #2
    Re: DOM child finding


    "skrebbel" <e.pear.teeseli nk@student.appl e.tue.banana.nl wrote in message
    news:op.tq95l61 r18tlcy@s011169 .mshome.net...
    Is there any faster/easier way of finding a child node of a certain
    DOMNode object than doing something like

    function getChildByTagna me($curnode, $name)
    {
    foreach($curnod e->childNodes as $node)
    {
    if($node->nodeName==$nam e)
    {
    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.



    Comment

    • skrebbel

      #3
      Re: DOM child finding

      On Tue, 24 Apr 2007 11:55:27 +0200, Sean
      <sean.anderso n@[nospam]oakleafgroup.bi zwrote:
      >
      "skrebbel" <e.pear.teeseli nk@student.appl e.tue.banana.nl wrote in
      message
      news:op.tq95l61 r18tlcy@s011169 .mshome.net...
      Is there any faster/easier way of finding a child node of a certain
      DOMNode object than doing something like
      >
      function getChildByTagna me($curnode, $name)
      {
      foreach($curnod e->childNodes as $node)
      {
      if($node->nodeName==$nam e)
      {
      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>Banana s, and how to peel them</title>
      <author>John Doe</author>
      <review>
      <author>Perry White</author>
      <title>New banana peeling book revolutionalise s 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->getElementsByT agName 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

      Comment

      Working...