|
Outputting XML with PHP: DOM or non_DOM?
Does anyone know is there a difference or a reason why to use a dom parent/child approach to writing an xml file from php as opposed to just writing the full set of nested tag? Is there a difference in the usability of the output file?
Here is a snippet of an example of what I mean by non-dom xml output and under it is a snippet of an example of what I mean by dom output. This question has relevance mostly because I am using a php 4 server where the nice functions for the dom approach are not available (or if there are some others, I cannot find them documented).
non-DOM approach:
echo "<?xml version=\"1.0\" ?>\n";
echo "<gallery>\n";
echo "<Images>$numLogos</Images>\n";
echo "<Image>\n";
for($i=1 ; $i <= $numLogos ; $i++)
{
echo "<ImagePath$i>$directory".$filearray[$i]."</ImagePath$i>\n";
}
echo "</Image>\n";
echo "</gallery>\n";
DOM approach
$doc = new DomDocument("1.0", "utf-8");
$node = $doc->createElement("gallery");
$parnode = $doc->appendChild($node);
$node = $doc->createElement("settings");
$settingsNode = $parnode->appendChild($node);
$node = $doc->createElement("mediaFolder");
$mediaFolderLargeNode = $settingsNode->appendChild($node);
$mediaFolderLargeNode->setAttribute("type", "large");
$mediaFolderLargeNode->setAttribute("media", "image");
$mediaFolderLargeNode->nodeValue = "images/";
etc.
G
|