To save to a file, simply use the string buffer produced by $doc->dump_mem(true) in the example to your file using fopen/fwrite/fclose.
Here is an example with this change:
$doc = domxml_new_doc("1.0");
$root = $doc->create_element("HTML");
$root = $doc->append_child($root);
$head = $doc->create_element("HEAD");
$head = $root->append_child($head);
$title = $doc->create_element("TITLE");
$title = $head->append_child($title);
$text = $doc->create_text_node("This is the title");
$text = $title->append_child($text);
$xml_data = $doc->dump_mem(true);
$filename = 'test.xml'; //change this to the filepath/name you like
// Let's make sure the file exists and is writable first.
if (is_writable($filename)) {
if (!$handle = fopen($filename, 'wb')) {
echo "Cannot open file ($filename)";
exit;
}
// Write $xml_data to our opened file.
if (fwrite($handle, $xml_data) === FALSE) {
echo "Cannot write to file ($filename)";
exit;
}
echo "Success, wrote ($somecontent) to file ($filename)";
fclose($handle);
} else {
echo "The file $filename is not writable";
A more complete solution to your problem would involve more extensive PHP code. Several complete implementations can be found on other PHP sites (for example, take a look at
www.phpclasses.org).
Philibuster