DOMImplementation::createDocument

(PHP 5, PHP 7, PHP 8)

DOMImplementation::createDocument 指定した型とドキュメント要素の DOMDocument オブジェクトを作成する

説明

public DOMImplementation::createDocument(?string $namespace = null, string $qualifiedName = "", ?DOMDocumentType $doctype = null): DOMDocument

指定した型とドキュメント要素の DOMDocument オブジェクトを作成します。

パラメータ

namespace

作成するドキュメント要素の名前空間 URI。

qualifiedName

作成するドキュメント要素の修飾名。

doctype

作成するドキュメントの型、あるいは null

戻り値

新しい DOMDocument オブジェクトを返します。 namespacequalifiedName および doctype が null の場合は、 ドキュメント要素を含まない空の DOMDocument を返します。

エラー / 例外

DOM_WRONG_DOCUMENT_ERR

doctype が既に別のドキュメントで使用されていたり、 別の実装で作成されている場合に発生します。

DOM_NAMESPACE_ERR

namespace および qualifiedName で指定した名前空間に間違いがある場合に発生します。

変更履歴

バージョン 説明
8.4.0 この関数の仮の戻り値の型が、DOMDocument になりました。
8.0.3 namespace は、nullable になりました。
8.0.0 doctype は、nullable になりました。
8.0.0 この関数を static メソッドとしてコールすると、 Error がスローされるようになりました。 これより前のバージョンでは、 E_DEPRECATED が発生していました。

参考

add a note

User Contributed Notes 3 notes

up
6
eboyjr
15 years ago
To add on to the other example, here's how to create an XHTML 1.0 transitional document with head, title, and body elements.<?php$document = DOMImplementation::createDocument(null, 'html',    DOMImplementation::createDocumentType("html",         "-//W3C//DTD XHTML 1.0 Transitional//EN",         "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"));$document->formatOutput = true;$html = $document->documentElement;$head = $document->createElement('head');$title = $document->createElement('title');$text = $document->createTextNode('Title of Page');$body = $document->createElement('body');$title->appendChild($text);$head->appendChild($title);$html->appendChild($head);$html->appendChild($body);echo $document->saveXML();?>This outputs: (http links removed due to spam)<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "doctype.dtd"> <html xmlns="w3org1999xhtml">   <head>     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />     <title>Title of Page</title>   </head>   <body></body> </html> Note the saveXML function. If saveHTML was used instead, you get the output:<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "doctype.dtd"> <html> <head><title>Title of Page</title></head> <body></body> </html>
up
1
arturm at union dot com dot pl
19 years ago
To create HTML document with doctype:

<?php
$doctype = DOMImplementation::createDocumentType("html",
                "-//W3C//DTD HTML 4.01//EN",
                "http://www.w3.org/TR/html4/strict.dtd");
$doc = DOMImplementation::createDocument(null, 'html', $doctype);
?>
up
-1
sleistico at gmail dot com
7 years ago
I just recently got an error, having to do with deprecation, by using the type of calls in the other example listed here.  What I had to do instead looks like this...$htmldoc = (new DOMImplementation)->createDocument(null, 'html', (new DOMImplementation)->createDocumentType("html"));This creates a document with <!DOCTYPE html> at the top of it.
To Top