Добавить DocType для выхода из DomDocument

1

У меня класс 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
domdocument
doctype

2 ответа

1
Лучший ответ

Вы можете использовать реализацию 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/>
0

Я бы использовал это

 <?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

Ещё вопросы

Сообщество Overcoder
Наверх
Меню