note 16947 added to function.domxml-new-xmldoc

From: Date: Mon, 19 Nov 2001 23:05:40 +0000
Subject: note 16947 added to function.domxml-new-xmldoc
Groups: php.notes 
Request: Send a blank email to php-notes+get-21033@lists.php.net to get a copy of this message
Creating pretty XML. Hope this helps someone out. -Dan Sturtevant Paralogic,Inc. In order to better view my generated xml I created the following function. It's a bit ugly, but it works: used the new_xmldoc() function to cr eate a string of of XML like so: $doc = new_xmldoc("1.0"); ... $xml_string = $doc->dumpmem(); // $xml_string is single line of xml. $xml_string = chop($xml_string); //acts like perl's chomp; Then passed the string to this function: function GenPrettyXML(&$xml_string) { $pretty_string = ""; //now we split this into an array for each line to make human readable. $xml_array = preg_split ('/></', $xml_string); //cycle through array and put < and > back into the xml $xml_array[0] = $xml_array[0].">"; for ($i = 1; $i < count($xml_array); $i++) { if($i != (count($xml_array) - 1)){ $xml_array[$i] = "<".$xml_array[$i].">"; }else{ $xml_array[$i] = "<".$xml_array[$i]; } } //now the xml array is correct. //to output the xml with indentation we need to do some regexp matches //to see where to indent $tabcount = 0; for ($i = 0; $i < count($xml_array); $i++) { if (ereg("<.*/>$", $xml_array[$i],$same1) or ereg("^<.*>.*</.*>$",$xml_array[$i],$same2)): for( $j = 0; $j < $tabcount; $j++ ) { $pretty_string = $pretty_string."\t"; } elseif (ereg("</.*>", $xml_array[$i], $same3)): $tabcount--; for( $j = 0; $j < $tabcount; $j++ ) { $pretty_string = $pretty_string."\t"; } elseif (ereg("<.*>", $xml_array[$i], $same4)): for( $j = 0; $j < $tabcount; $j++ ) { $pretty_string = $pretty_string."\t"; } $tabcount++; endif; $pretty_string = $pretty_string.$xml_array[$i]."\n"; } $xml_string = $pretty_string; } -- http://www.php.net/manual/en/function.domxml-new-xmldoc.php http://master.php.net/manage/user-notes.php?action=edit+16947 http://master.php.net/manage/user-notes.php?action=delete+16947 http://master.php.net/manage/user-notes.php?action=reject+16947

« previous php.notes (#21033) next »