How to create a document in PHP: Use dom_create_document() to create a new XML document object. Use dom_create_element() to create a new XML element object. Use dom_append_child() to append elements to the document. Use dom_create_text_node() to create a text node. Use dom_append_child() to append text nodes to elements. Use saveXML() to save the document.
How to Create Documents in PHP
PHP provides various functions to help create documents that are easy to use and functional powerful.
dom_create_document()
This function creates a new XML document object.
$doc = dom_create_document(null, null, null);
dom_create_element()
This function creates a new XML element object.
$root = $doc->createElement('root');
dom_append_child()
This function appends one XML element to another XML element.
$doc->appendChild($root);
dom_create_text_node()
This function creates a new XML text node.
$text = $doc->createTextNode('Hello, world!');
dom_append_child()
This function appends an XML text node to an XML element.
$root->appendChild($text);
Save the document
Use the saveXML()
method to save the document to a file.
$doc->saveXML('document.xml');
Practical case
The following code creates an XML document named document.xml
, which contains a root
element and a text node.
$doc = dom_create_document(null, null, null); $root = $doc->createElement('root'); $doc->appendChild($root); $text = $doc->createTextNode('Hello, world!'); $root->appendChild($text); $doc->saveXML('document.xml');
The above is the detailed content of How do PHP functions create documents?. For more information, please follow other related articles on the PHP Chinese website!