Home » PHP, XML23 October 2009

PHP : Read Write Xml with DOMDocument

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

The DOM extension allows you to operate on XML documents through the DOM API with PHP 5.

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_dom.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.

XML

Get New Post by Email

Find me on

FacebookTwitterGoogle+LinkedInRSS Feed
  • Hi Mukesh,
    I want to print the XML output in browser.
    Thanks

  • Bacurastone

    help me,

    i used php DomDocument to read my xml file from external server, i’am using php vser 5.2.6
    my php script is like this
    $dom=new domDocument(“1.0”);
    $dom->load(“http://mysite.com/file/xml”);

    i can display my xml data with out any error.
    but, when xml data it’s updated (mean that i add some node on it).
    my script cannot display update item. like no chage have done in my xml file.
    i don’t know what this problem!
    is there is something wrong with my php.ini file configuration.
    help me please!

    regard saiful anwar.
    thank in advance!