Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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'); 
$objcDom->fromMixed($this->data);

How I can add an DocType to $objcDom?

like image 364
pet21 Avatar asked Dec 13 '25 23:12

pet21


1 Answers

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();
$dom->appendChild($implementation->createDocumentType('example'));
$dom->appendChild($dom->createElement('foo'));

echo $dom->saveXml();

Output:

<?xml version="1.0"?>
<!DOCTYPE example>
<foo/>
like image 185
ThW Avatar answered Dec 15 '25 11:12

ThW



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!