Home » PHP, XML23 October 2009

PHP : Read Write Xml with SimpleXML

In this article, I will be showing you how to create and read xml document with php’s SimpleXML extension.

The SimpleXML extension provides a very simple and easily usable toolset to convert XML to an object that can be processed with normal property selectors and array iterators.

Below is the code to create XML file. I have named the output file ‘example.xml’. For better understanding, I have written comment for almost every line of code.

The following xml file is created. The xml file is named example.xml.

Here is the code to read the xml file.
I have mentioned two ways to read the xml file. The first one loads the xml file, converts the data into string and prints it.

The second way is to loop through the xml content and print. It’s more specific.

Hope it helps. Thanks.


Get New Post by Email

Find me on

FacebookTwitterGoogle+LinkedInRSS Feed
  • guest


  • Omsakthi_ai

    this is not working
    Root Element:

    “Fatal error: Call to a member function getName() on a non-object in C:wampwwwtestxmltest.php on line 8”

    Above message i got. Do you have any idea…