XML DOM Parser

XML DOM Parser


Posted in : PHP Posted on : January 21, 2011 at 5:36 PM Comments : [ 0 ]

This section contains the detail about the XML DOM Parser in PHP.

XML DOM Parser

Each XML document can be classified into a standard set of objects using DOM(Document Object modeling).

As we discussed earlier in previous tutorial that XML parsers can be classified into two groups : First is 1. Tree-based parser and Second is 2. Event-based parser.

The XML DOM parser is an Tree-based parser. Using Tree-based parser, an XML document is transforms into a tree structure. After analyzing and transforming the XML document , it also provides access to the tree elements.

For an XML document to be valid(when using XML DOM parser), the Document Type Definition (DTD) must be associated with it.

Let us take a simple XML :

<?xml version="1.0" encoding="ISO-8859-1"?>
<fname>Ankit</fname>

The DOM parser transforms the above XML into the given below tree structure :

                                  Level 1 : XML document

                                                 ↓

                                  Level 2  : Root Element : <fname>

                                                ↓ 

                                  Level 3  :  Text element: "Ankit"

Note : There is no need of extra installation for XML DOM parser for PHP because its methods are inbuilt in PHP  core.

Example :

The XML file(demo.xml) is given below :

<?xml version="1.0" encoding="ISO-8859-1"?>
<phonebook>
<name>Ankit</name>
<surname>Kaushal</surname>
<mobile>99999999</mobile>
<location>New Delhi</location>
</phonebook>

DOMparser.php

<?php
$xmlDoc = new DOMDocument();
$xmlDoc->load("XML/demo.xml");

print $xmlDoc->saveXML();
?>

Output :

The output of the above PHP code will be :

Download Source Code

Go to Topic «PreviousHomeNext»

Your Comment:


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

 
Tutorial Topics