InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more

Menu
  • Home
  • Sitemap

Python Programming Language Best Tutorials and Code Examples

Learn Python Right Now!
Home
PHP
Generating XML with DOM in PHP
PHP

Generating XML with DOM in PHP

InfinityCoder December 6, 2016

You want to generate XML but want to do it in an organized way instead of using print and loops.

Use the DOM extension to create a DOMDocument object. After building up the document, call DOMDocument::save() or DOMDocument::saveXML() to generate a well-formed XML document:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
// create a new document
$dom = new DOMDocument('1.0');
 
// create the root element, <book>, and append it to the document
$book = $dom->appendChild($dom->createElement('book'));
 
// create the title element and append it to $book
$title = $book->appendChild($dom->createElement('title'));
 
// set the text and the cover attribute for $title
$title->appendChild($dom->createTextNode('PHP Cookbook'));
$title->setAttribute('edition', '3');
 
// create and append author elements to $book
$sklar = $book->appendChild($dom->createElement('author'));
 
// create and append the text for each element
$sklar->appendChild($dom->createTextNode('Sklar'));
$trachtenberg = $book->appendChild($dom->createElement('author'));
$trachtenberg->appendChild($dom->createTextNode('Trachtenberg'));
 
// print a nicely formatted version of the DOM document as XML
$dom->formatOutput = true;
echo $dom->saveXML();
 
<?xml version="1.0"?>
<book>
  <title edition="3">PHP Cookbook</title>
  <author>Sklar</author>
  <author>Trachtenberg</author>
</book>

The DOM methods follow a pattern. You create an object as either an element or a text node, add and set any attributes you want, and then append it to the tree in the spot it belongs.
Before creating elements, create a new document, passing the XML version as the sole argument:

1
$dom = new DOMDocument('1.0');

Now create new elements belonging to the document. Despite being associated with a specific document, nodes don’t join the document tree until appended:

1
2
$book_element = $dom->createElement('book');
$book = $dom->appendChild($book_element);

Here a new book element is created and assigned to the object $book_element. To create the document root, append $book_element as a child of the $dom document.

The result, $book, refers to the specific element and its location within the DOM object. All nodes are created by calling a method on $dom.

Once a node is created, it can be appended to any element in the tree. The element from which we call the append
Child() method determines the location in the tree where the node is placed.

In theprevious case, $book_element is appended to $dom. The element appended to $dom is the top-level node, or the root node.
You can also append a new child element to $book. Because $book is a child of $dom, the new element is, by extension, a grandchild of $dom:

1
2
$title_element = $dom->createElement('title');
$title = $book->appendChild($title_element);

By calling $book->appendChild(), this code places the $title_element element under the $book element.
To add the text inside the <title></title> tags, create a text node using createText Node() and append it to $title:

1
2
$text_node = $dom->createTextNode('PHP Cookbook');
$title->appendChild($text_node);

Because $title is already added to the document, there’s no need to reappend it to $book.
The order in which you append children to nodes isn’t important.

The following four lines, which first append the text node to $title_element and then to $book, are equivalent
to the previous code:

1
2
3
4
5
$title_element = $dom->createElement('title');
$text_node = $dom->createTextNode('PHP Cookbook');
 
$title_element->appendChild($text_node);
$book->appendChild($title_element);

To add an attribute, call setAttribute() upon a node, passing the attribute name and value as arguments:

1
$title->setAttribute('edition', '3');

If you print the title element now, it looks like this:

1
<title edition="3">PHP Cookbook</title>

Once you’re finished, you can output the document as a string or to a file:

1
2
3
4
5
// put the string representation of the XML document in $books
$books = $dom->saveXML();
 
// write the XML document to books.xml
$dom->save('books.xml');

By default, these methods generate XML output in one long line without any whitespace, including indentations and line breaks.

To fix this, set the formatOutput attribute of your DOMDocument to true:

1
2
// print a nicely formatted version of the DOM document as XML
$dom->formatOutput = true;

This causes the DOM extension to generate XML like this:

1
2
3
4
<?xml version="1.0"?>
<book>
  <title cover="soft">PHP Cookbook</title>
</book>

 

Share
Tweet
Email
Prev Article
Next Article

Related Articles

Transforming XML with XSLT in PHP
You have an XML document and an XSL stylesheet. You …

Transforming XML with XSLT in PHP

Extracting Information Using XPath in PHP
You want to make sophisticated queries of your XML data …

Extracting Information Using XPath in PHP

About The Author

InfinityCoder
InfinityCoder

Leave a Reply

Cancel reply

Recent Tutorials InfinityQuest

  • Adding New Features to bash Using Loadable Built-ins in bash
    Adding New Features to bash Using Loadable …
    June 27, 2017 0
  • Getting to the Bottom of Things in bash
    Getting to the Bottom of Things in …
    June 27, 2017 0

Recent Comments

  • fer on Turning a Dictionary into XML in Python
  • mahesh on Turning a Dictionary into XML in Python

Categories

  • Bash
  • PHP
  • Python
  • Uncategorized

InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more

About Us

Start learning your desired programming language with InfinityQuest.com.

On our website you can access any tutorial that you want with video and code examples.

We are very happy and honored that InfinityQuest.com has been listed as a recommended learning website for students.

Popular Tags

binary data python CIDR convert string into datetime python create xml from dict python dictionary into xml python how to create xml with dict in Python how to write binary data in Python IP Address read binary data python tutorial string as date object python string to datetime python

Archives

  • June 2017
  • April 2017
  • February 2017
  • January 2017
  • December 2016
  • November 2016
Copyright © 2021 InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more
Programming Tutorials | Sitemap