Creating an XML using DOM extension
DOM
extension gives us the ability to create whole new documents using its numerous functions. In this recipe you will learn how to create new XML documents using DOM functions. As you know we have multiple book elements in our common.xml
file, we will create a similar book element with name and story elements using DOM methods.
Getting ready
Create a new folder Recipe5
in the Chapter3
directory.
How to do it…
Create a file and name it
index.php
in theRecipe5
folder.Write the PHP code that will create a new XML document, then create some elements and add these to the new document. Some of these elements will have text as well as attributes and their values. Finally, this XML will be saved on the disk.
<?php $objXML = new DOMDocument('1.0', 'utf-8'); /* <?xml version="1.0" encoding="UTF-8" ?> */ $books = $objXML->createElement('books');//books $book = $objXML->createElement('book'); $attrIndex = new DOMAttr("index", "4"); $book...