| re: PHP, DOM, XML questions..
John VanRyn wrote:[color=blue]
> #1 How do you set "<!DOCTYPE graph SYSTEM "graph.dtd">"?
>[/color]
<?php
// Create an instance of the DomImplementation class
$impl = new DomImplementation;
// Create an instance of the DomDocumentType class by
// calling the DomImplementation::createDocumentType() method
// (arguments: doctype_name, doctype_public, doctype_system)
$dtd = $impl->createDocumentType("graph", "", "graph.dtd");
// Create an instance of the DomDocument class by
// calling the DomImplementation::createDocument method
// (arguments: NS_URL, NS_PREFIX, DomDocumentType_instance)
$dom = $impl->createDocument("", "", $dtd);
// Set other properties
$dom->encoding = "UTF-8";
$dom->standalone = "no";
// Create an empty element
$element = $dom->createElement("graph");
// Append the element
$dom->appendChild($element);
// Retrieve and print the document
echo "<pre>", htmlentities($dom->saveXML()), "</pre>";
?>
[color=blue]
> #2 How to you modify an existing node?[/color]
This can be done with the replaceChild method:
// Get the node list
$nodelist = $dom->getElementsByTagName("graph");
// Get the node and copy it
$old_element = $nodelist->item(0);
$new_element = $old_element;
// Set the attribute for the new element
$new_element->setAttribute("foo","bar");
// Replace it
$dom->replaceChild($old_element, $new_element);
HTH;
JW |