pet21 pet21 - 4 months ago 7x
PHP Question

Add DocType to exits DomDocument

i have a Php class likes "Extension_DOMDocument" and this extends the PHP "DOMDocument" class.

I create a new Object of Extension_DOMDocument and would add to DocType to this Object.

My code is:

// $this->data is an array to convert array to xml
$objcDom = new Extension_DOMDocument('1.0', 'utf-8');

How i can add an DocType to $objcDom?

Thank you!


You can use the the DOM implementation to create a document type object. Document type objects are still DOM nodes. You can append them to an existing document.

class MyDOMDocument extends DOMDocument {}

$dom = new MyDOMDocument();
$implementation = new DOMImplementation();

echo $dom->saveXml();


<?xml version="1.0"?>
<!DOCTYPE example>