DOMImplementation::createDocument

(PHP 5, PHP 7, PHP 8)

DOMImplementation::createDocument Crée un objet DOM Document du type spécifié avec ses éléments

Description

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

Crée un objet DOMDocument du type spécifié avec ces éléments du document.

Liste de paramètres

namespace

L'URI de l'espace de noms des éléments du document à créer.

qualifiedName

Le nom qualifié des éléments du document à créer.

doctype

Le type de document à créer ou null.

Valeurs de retour

Un nouvel objet DOMDocument. Si namespace, qualifiedName, et doctype sont nulles, le DOMDocument retourné est vide avec aucun élément de document.

Erreurs / Exceptions

DOM_WRONG_DOCUMENT_ERR

Lancé si doctype a déjà été utilisé avec un document différent ou a été créé depuis une implémentation différente.

DOM_NAMESPACE_ERR

Lancé s'il y a une erreur dans l'espace de noms, déterminé par namespace et qualifiedName.

Cette méthode peut être appelée statiquement, mais enverra une erreur E_STRICT.

Historique

Version Description
8.0.3 namespace est désormais nullable.
8.0.0 doctype est désormais nullable.

Voir aussi

add a note add a note

User Contributed Notes 3 notes

up
4
eboyjr
13 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
0
arturm at union dot com dot pl
17 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
5 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