Nuri Ensing
Nuri Ensing

Reputation: 2030

adding doctype in xml with php domdocument

In php I create my XML using the domdocument:

$xml_data = new DOMDocument("1.0", "UTF-8");

// create root element
$root = $xml_data->createElement("cXML");
$xml_data->appendChild($root);

etcetera

Is it possible to add this element:

<!DOCTYPE cXML
  SYSTEM 'http://xml.cXML.org/schemas/cXML/1.2.008/cXML.dtd'>

In to the XML with PHP domdocument, so you get:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE cXML
  SYSTEM 'http://xml.cXML.org/schemas/cXML/1.2.008/cXML.dtd'>

Upvotes: 3

Views: 1450

Answers (1)

Alex Blex
Alex Blex

Reputation: 37038

You can use DOMImplementation instead:

$imp = new DOMImplementation();
$xml_data = $imp->createDocument(
    null, 
    'cXML',  
    $imp->createDocumentType('cXML', 
        null, 
        'http://xml.cXML.org/schemas/cXML/1.2.008/cXML.dtd'
    )
);
$xml_data->xmlVersion="1.0";
$xml_data->encoding="UTF-8";

// No need to create root elemet, it's already there
// $root = $xml_data->createElement("cXML");
// $xml_data->appendChild($root);

etcetera

Upvotes: 2

Related Questions