DOMImplementation::createDocument

(PHP 5, PHP 7, PHP 8)

DOMImplementation::createDocument β€” Π‘ΠΎΠ·Π΄Π°Ρ‘Ρ‚ ΠΎΠ±ΡŠΠ΅ΠΊΡ‚ класса DOMDocument Π·Π°Π΄Π°Π½Π½ΠΎΠ³ΠΎ Ρ‚ΠΈΠΏΠ° с Π΅Π³ΠΎ элСмСнтом document

ОписаниС

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

Π‘ΠΎΠ·Π΄Π°Ρ‘Ρ‚ ΠΎΠ±ΡŠΠ΅ΠΊΡ‚ класса DOMDocument Π·Π°Π΄Π°Π½Π½ΠΎΠ³ΠΎ Ρ‚ΠΈΠΏΠ° с Π΅Π³ΠΎ элСмСнтом document.

Бписок ΠΏΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€ΠΎΠ²

namespace

URI пространства ΠΈΠΌΡ‘Π½ создаваСмого элСмСнта document.

qualifiedName

ΠšΠ²Π°Π»ΠΈΡ„ΠΈΡ†ΠΈΡ€ΠΎΠ²Π°Π½Π½ΠΎΠ΅ имя создаваСмого элСмСнта document.

doctype

Π’ΠΈΠΏ создаваСмого элСмСнта document ΠΈΠ»ΠΈ null.

Π’ΠΎΠ·Π²Ρ€Π°Ρ‰Π°Π΅ΠΌΡ‹Π΅ значСния

Новый ΠΎΠ±ΡŠΠ΅ΠΊΡ‚ класса DOMDocument. Если Π°Ρ€Π³ΡƒΠΌΠ΅Π½Ρ‚Ρ‹ namespace, qualifiedName, ΠΈ doctype ΠΈΠΌΠ΅ΡŽΡ‚ Π·Π½Π°Ρ‡Π΅Π½ΠΈΠ΅ null, Π²ΠΎΠ·Π²Ρ€Π°Ρ‰Π°Π΅ΠΌΡ‹ΠΉ ΠΎΠ±ΡŠΠ΅ΠΊΡ‚ DOMDocument Π±ΡƒΠ΄Π΅Ρ‚ пустым ΠΈ Π±Π΅Π· элСмСнта document.

Ошибки

ΠœΠΎΠΆΠ΅Ρ‚ Π²Ρ‹Π±Ρ€ΠΎΡΠΈΡ‚ΡŒ ΠΈΡΠΊΠ»ΡŽΡ‡Π΅Π½ΠΈΠ΅ DOMException со ΡΠ»Π΅Π΄ΡƒΡŽΡ‰ΠΈΠΌΠΈ ΠΊΠΎΠ΄Π°ΠΌΠΈ ошибок:

DOM_WRONG_DOCUMENT_ERR

Π’ΠΎΠ·Π½ΠΈΠΊΠ°Π΅Ρ‚, Ссли Π°Ρ€Π³ΡƒΠΌΠ΅Π½Ρ‚ doctype ΡƒΠΆΠ΅ использовался с Π΄Ρ€ΡƒΠ³ΠΈΠΌ Π΄ΠΎΠΊΡƒΠΌΠ΅Π½Ρ‚ΠΎΠΌ ΠΈΠ»ΠΈ Π±Ρ‹Π» создан Π² Π΄Ρ€ΡƒΠ³ΠΎΠΉ Ρ€Π΅Π°Π»ΠΈΠ·Π°Ρ†ΠΈΠΈ.

DOM_NAMESPACE_ERR

Π’ΠΎΠ·Π½ΠΈΠΊΠ°Π΅Ρ‚, Ссли ΠΎΠ±Π½Π°Ρ€ΡƒΠΆΠ΅Π½Π° ошибка Π² строках namespace ΠΈ qualifiedName.

Бписок измСнСний

ВСрсия ОписаниС
8.4.0 Π’Π΅ΠΏΠ΅Ρ€ΡŒ Ρƒ Ρ„ΡƒΠ½ΠΊΡ†ΠΈΠΈ ΠΏΡ€Π΅Π΄Π²Π°Ρ€ΠΈΡ‚Π΅Π»ΡŒΠ½Ρ‹ΠΉ Π²ΠΎΠ·Π²Ρ€Π°Ρ‰Π°Π΅ΠΌΡ‹ΠΉ Ρ‚ΠΈΠΏ DOMDocument.
8.0.3 namespace Ρ‚Π΅ΠΏΠ΅Ρ€ΡŒ допускаСт Π·Π½Π°Ρ‡Π΅Π½ΠΈΠ΅ null.
8.0.0 doctype Ρ‚Π΅ΠΏΠ΅Ρ€ΡŒ допускаСт Π·Π½Π°Ρ‡Π΅Π½ΠΈΠ΅ null.
8.0.0 ΠŸΡ€ΠΈ статичСском Π²Ρ‹Π·ΠΎΠ²Π΅ Ρ„ΡƒΠ½ΠΊΡ†ΠΈΠΈ Ρ‚Π΅ΠΏΠ΅Ρ€ΡŒ Π±ΡƒΠ΄Π΅Ρ‚ Π²Ρ‹Π±Ρ€Π°ΡΡ‹Π²Π°Ρ‚ΡŒΡΡ ошибка Error. Π Π°Π½Π΅Π΅ Π²Ρ‹Π΄Π°Π²Π°Π»Π°ΡΡŒ ошибка уровня E_DEPRECATED.

Π‘ΠΌΠΎΡ‚Ρ€ΠΈΡ‚Π΅ Ρ‚Π°ΠΊΠΆΠ΅

οΌ‹Π”ΠΎΠ±Π°Π²ΠΈΡ‚ΡŒ

ΠŸΡ€ΠΈΠΌΠ΅Ρ‡Π°Π½ΠΈΡ ΠΏΠΎΠ»ΡŒΠ·ΠΎΠ²Π°Ρ‚Π΅Π»Π΅ΠΉ 3 notes

up
9
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
2
arturm at union dot com dot pl ΒΆ
20 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
0
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.