Creating an XML in PHP using DOMDocument
In this tutorial you will learn how to create XML documents using PHP’s DOMDocument class.
First things first.There are 3 things that you should always keep in mind about XML. In short, XML has 3 main components.
- Elements, which we also call nodes. e.g.
- Attributes e.g
Here type is an attribute.
- Data/text nodes
Johnson Here “first” and “Johnson” are text nodes.
Hope this is clear. Now, we will create the following XML from our code.
<?xml version="1.0" encoding="utf-8"?>100 grams 43 3.2 grams
Recently, I have become too much conscious(obsessed!!!) about calories, fat and protein. Hence this XML.
Never mind, let us jump to code now.
First of all we will create an empty xml doxument. Then we will append nodes to it as required.
$objXML = new DOMDocument('1.0','utf-8');
This is constructor of DOMDocument class which defines an XML document. It will create an empty xml document. 1.0 is version and utf-8 is encoding of resulting xml. Second parameter is optional.
Total 4 functions will be used to create the xml.
- createElement: used to create xml nodes.
- createAttribute: used to create attributes of a node.
- createTextNode: used to create text nodes or createCDATASection if you want cdata sections instead of text nodes.
- appendChild: this will append above mentioned elements to xml.
First, let us create our root node fruit with attribute name=apple.
$fruit = $objXML->createElement("fruit");
$objXML->appendChild($fruit);
$fruitName = $objXML->createAttribute("name");
$fruitName->appendChild($objXML->createTextNode("Apple"));
$fruit->appendChild($fruitName);
Line 1 creates an element named fruit. Line 2 appends fruit to xml document. Line 3 creates an attribute called name. Line 4 sets the value of attribute name to “Apple” and finally line 5 appends this attribute to fruit element.
To see this xml write the following.
echo $objXML->saveXML();
This will create following xml:
Looks good. XML with an empty node fruit has been created(If xml is not visible in your browser, view the source of page). Now all we have to do is create elements weight, calories, protein, fat and append them to parent node fruit. Lets do it.
//create element weight
$weight = $objXML->createElement("weight");
//append text to it by creating a text node inside it
//you can use createCDATASection if you need it
$weight->appendChild($objXML->createTextNode("100 grams"));
//append newly created weight node to fruit node
$fruit->appendChild($weight);
Comments aboveare self explanatory. Now repeat the same procedure to create remaining nodes.
//create element calories
$calories = $objXML->createElement("calories");
//append text to it by creating a text node inside it
$calories->appendChild($objXML->createTextNode("43"));
//append newly created calories node to fruit node
$fruit->appendChild($calories);
//create element protein
$protein = $objXML->createElement("protein");
//append text to it by creating a text node inside it
$protein->appendChild($objXML->createTextNode("3.2 grams"));
//append newly created calories node to fruit node
$fruit->appendChild($protein);
Note: Weight, calories and protein nodes are identical. Above is only an example. However, in real world scenes, you should create a function to avoid duplication of code.
All done. Only thing thing remaining is saving this XML. There are 2 ways to do this.
echo $objXML->saveXML();
will return a string containing created xml.
$objXML->save('someName.xml');
will save the xml to someName.xml file.
Resulting xml will be
<?xml version="1.0" encoding="utf-8"?>100 grams 43 3.2 grams
This is it for now. Complete source code is available at this link. If you are thinking why we wrote 16 lines of code to generate a 5 line xml, think again. In next post I will show you how can you create large xmls (say of 500 lines ) with 25 lines of code.
Do let me know if you have any queries.

thanks so much for this tutorial.it’s very useful for me but i want to ask you.i tried this code and each time i run this code it overwrites the old values not appending.i want each time i run this code i add a new node with the old nodes not deleting them by overwriting like in your example i want to create many fruit nodes.thanks again for this nice tutorial.