Adding a Doctype Declaration to a DOMDocument in PHP

Posted in PHP and Programming on Monday, the 9th of March, 2009.

Tagged: , , , and

I've recently been spending quite a lot of time with PHP's DOM extension, which is extremely useful for both generating and parsing XML.

In this particular case, I'm generating XML, and it's imperative that the XML markup which the code is generating should contain a Doctype declaration (DTD). It isn't hard to do that using DOM, but it did take a little bit of hunting around in the manual and online, so here's a quick overview of how to add a Doctype declaration to a DOMDocument.

Comments

Posted by José on Friday, the 6th of November, 2009.

Hi, and thanks for its post, really help me so much to made something like that, but i have a doubt. How can insert a DocType to a DOMDocument that i already load from a XML file whit $domdocument->load('my.xml')?
Its file have not a doctype declared, but for make a validation i need insert it.
Can you give me some idea?

Posted by Ciaran McNulty on Friday, the 6th of November, 2009.

@José - You could create a new document, then import the nodes from the existing document into it:

$child = $yourDom->importNode($simonsDom->documentElement);
$yourDom->documentElement->appendChild($child, true);

Posted by José on Friday, the 6th of November, 2009.

Thanks again for the answer & time, i do this:
$node = $documentXML->importNode($loadedXML->documentElement, true);
//$documentXML is an empty XML Document $documentXML->appendChild($node);

but the result isn't the expected:
when echo $documentXML->saveXML() i see some changes in the structure of the XML's tree and its element's attributes and values; sorry for the necesary question. But, are you some idea why is it?

[comment snipped]

Posted by Simon Harris on Tuesday, the 10th of November, 2009.

José -

I've snipped your later comments as they didn't display as I assume you hoped they might. They also contained your email address in plain text.

You'll probably have more luck asking questions like yours over at Stack Overflow - you'll find a much larger audience there, and many of the people there really have a lot of time on their hands, so you're sure to find help.

Posted by Paulo Fonseca on Tuesday, the 13th of April, 2010.

Hi, i used your code in the following way:

<?php

$implementation = new DOMImplementation();

$dtd = $implementation->createDocumentType('ementa', '', 'ementa.dtd');

$document = $implementation->createDocument('', '', $dtd);

//$document = new DOMDocument('1.0', 'UTF-8');

$document->formatOutput = true;

//Cria a Root
$root = $document->createElement( "ementa" );
$document->appendChild( $root );

This will create the xml file with the correct DOCTYPE.

My question:

The 1st header generated is:

<?xml version="1.0"?>

How can i add encoding info to this header to make it like this:

<?xml version="1.0" encoding="UTF-8"?>

Many Thanks for your help!

Posted by Windigo on Sunday, the 1st of August, 2010.

Thanks so much; this was "less than obvious" in the PHP documentation at the best, and exactly what I was looking for!

Posted by Terence Simpson on Saturday, the 20th of November, 2010.

@Paulo
You can set the encoding after creating the document node with:
$document->encoding = 'utf-8';

Posted by nickl- on Sunday, the 3rd of June, 2012.

After some deliberation and lots of persistence I managed to change the doctype on render this is what the render function looks like:


public function render($doctype='')
{
if ($doctype) {
$doc = new DOMDocument();
$doc->loadHTML($doctype);
$dt = $doc->doctype;
$di = new DOMImplementation();
$dt = $di->createDocumentType($dt->name, $dt->publicId, $dt->systemId);
$this->dom->replaceChild($dt, $this->dom->doctype);
}
return preg_replace('/\n/', '', $this->dom->saveXML());
}
Hope that helps =)

Enter your comment: