note 48405 added to ref.domxml

From: Date: Wed, 22 Dec 2004 13:54:46 +0000
Subject: note 48405 added to ref.domxml
Groups: php.notes 
Request: Send a blank email to php-notes+get-82177@lists.php.net to get a copy of this message
I recently developed a script for parsing DHL XML transaction responses - finding it a pain in the rear to actually parse the XML and set my variables - it actually wasn't that hard once I figured it out - and it goes something like this... <?php // Use with a class containing functions set_attributes() and // set_data(). Use the following to set variables from the // resulting xml. $node is a dom xml object - in the first call // to loop, $node would be equal to the root document // element. function loop($node) { // set attribute tags here if ($node->has_attributes()) { $this->set_attributes($node); } // end if node has attributes if ($node->has_child_nodes()) { $this->loop($node->first_child()); } // end if node has child else { $this->set_data($node); } // end if node has no child // get next sibling $node = $node->next_sibling(); if ($node) { $this->loop($node); } // end if node } // end function loop ?> The code goes from the root element, if the element has attributes - it sets attribute variables. Second, it recursively proceeds to the lowest level element (no more children). Once that level has been reached, data variables are set. The next step goes to the next sibling of the element, if it exists. If the next sibling does not exists, the function is ended and the current element is returned to the parent element. The parent element is then checked for siblings. This process continues (as is with recursion) until the parent element is back at the root element, which is the end of the document. ---- Manual Page -- http://www.php.net/manual/en/ref.domxml.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+48405 Delete -- http://master.php.net/manage/user-notes.php?action=delete+48405&report=yes Reason: bad code -- http://master.php.net/manage/user-notes.php?action=delete+48405&report=yes&reason=bad+code Reason: spam -- http://master.php.net/manage/user-notes.php?action=delete+48405&report=yes&reason=spam Reason: useless example -- http://master.php.net/manage/user-notes.php?action=delete+48405&report=yes&reason=useless+example Reason: contains commercial links -- http://master.php.net/manage/user-notes.php?action=delete+48405&report=yes&reason=contains+commercial+links Reason: useless note -- http://master.php.net/manage/user-notes.php?action=delete+48405&report=yes&reason=useless+note Reject -- http://master.php.net/manage/user-notes.php?action=reject+48405&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#82177) next »