У меня класс Php нравится "Extension_DOMDocument", и это расширяет класс PHP "DOMDocument".
Я создаю новый Object Extension_DOMDocument и добавляю к этому объекту DocType.
Мой код:
// $this->data is an array to convert array to xml
$objcDom = new Extension_DOMDocument('1.0', 'utf-8');
$objcDom->fromMixed($this->data);
Как я могу добавить DocType в $ objcDom?
Спасибо!
Вы можете использовать реализацию DOM для создания объекта типа документа. Объекты типа документа все еще являются узлами DOM. Вы можете добавить их в существующий документ.
class MyDOMDocument extends DOMDocument {}
$dom = new MyDOMDocument();
$implementation = new DOMImplementation();
$dom->appendChild($implementation->createDocumentType('example'));
$dom->appendChild($dom->createElement('foo'));
echo $dom->saveXml();
Вывод:
<?xml version="1.0"?>
<!DOCTYPE example>
<foo/>
Я бы использовал это
<?php
// Creates an instance of the DOMImplementation class
$imp = new DOMImplementation;
// Creates a DOMDocumentType instance
$dtd = $imp->createDocumentType('graph', '', 'graph.dtd');
// Creates a DOMDocument instance
$dom = $imp->createDocument("", "", $dtd);
// Set other properties
$dom->encoding = 'UTF-8';
$dom->standalone = false;
// Create an empty element
$element = $dom->createElement('graph');
// Append the element
$dom->appendChild($element);
// Retrieve and print the document
echo $dom->saveXML();
?>
Проверьте: http://php.net/manual/en/domimplementation.createdocumenttype.php