note 16947 deleted from function.domxml-new-xmldoc by jimw
| From: | jimw@php.net | Date: | Sat, 08 Dec 2001 23:53:07 +0000 |
| Subject: | note 16947 deleted from function.domxml-new-xmldoc by jimw | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-22384@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;
}