note 48405 deleted from ref.domxml by sobak

From: Date: Sun, 04 Dec 2016 07:13:55 +0000
Subject: note 48405 deleted from ref.domxml by sobak
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-208022@lists.php.net to get a copy of this message
Note Submitter: ndaniel at bsi-scs dot com ---- 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.

« previous php.notes (#208022) next »