DOM parser creates a tree-based structure. DOM is a part of the PHP core. As it deals with small documents, it presents the output in a structured way. 

Example of XML DOM in PHP

// XML file
    $xmlBody = "<?xml version='1.0' encoding='UTF-8'?>
    <locations>
        <city1>New York</city1>
        <city2>Los Angeles</city2>
        <city3>Chicago</city3>
    </locations>";
    
    // Loading from XML file
<?php
    $xmlFile = new DOMDocument();
    $xmlFile ->load("path/file.xml");
    print $xmlFile ->saveXML();
?>

The output from above example is:

New york Los Angeles Chicago

Another example of XML DOM in PHP

// XML file
    $xmlBody = "<?xml version='1.0' encoding='UTF-8'?>
    <locations>
        <city1>New York</city1>
        <city2>Los Angeles</city2>
        <city3>Chicago</city3>
    </locations>";
    
    // Loading from XML file
<?php
    $xmlFile = new DOMDocument();
    $xmlFile ->load("path/file.xml");
    $i = $xmlFile ->documentElement;
    foreach ($i ->childNodes AS $teml)
    {
        print $temp ->nodeName . " = " . $temp ->nodeValue . "br />";
    }
?>

The output from the 2nd example is:

city1 = New York

city2 = Los Angeles

city3 = Chicago

 

›› go to examples ››