note 42695 deleted from function.domnode-child-nodes by sobak

From: Date: Tue, 22 Nov 2016 03:41:50 +0000
Subject: note 42695 deleted from function.domnode-child-nodes by sobak
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-207414@lists.php.net to get a copy of this message
Note Submitter: andrew at transitionkiteboarding dot com ---- Sometimes unless you know exactly where elements are it can be time consuming to pick each thing up with get_element_by_tagname calls. I used a quick little function that, given a structure such as: <customer> <firstname>Joe</firstname> <surname>Bloggs</surname> <age>24</age> <sex>Male</sex> ... </customer> it will return an associative array with the tag names as keys and the content as values. During the load I strip whitespace as well so it isn't confused by \n's creating blank nodes -- CODE -- $xmldoc = domxml_open_mem($xmlstring, DOMXML_LOAD_PARSING + //0 DOMXML_LOAD_COMPLETE_ATTRS + //8 DOMXML_LOAD_SUBSTITUTE_ENTITIES + //4 DOMXML_LOAD_DONT_KEEP_BLANKS //16 )); // get the node root and then the children of it. $noderoot = $xmldoc->document_element(); $childnodes = $noderoot->child_nodes(); // build the array foreach ($childnodes as $value) { echo $value->tagname . " : " . $value->get_content() . "<br>"; $nodeArray[$value->tagname] = $value->get_content(); } -- END CODE -- You can then return node array or start using it however you like in other functions just like a normal associative array then. $nodeArray['firstname'] == "Joe"

« previous php.notes (#207414) next »