---
title: "XML Document Object Model (DOM) in PHP"  
description: "Document Object Model (DOM) provides a standard set of objects for HTML and XML documents, and a standard interface for accessing and manipulating the"  
author: "Anonymous User"  
published: 2011-09-22  
updated: 2020-03-01  
canonical: https://www.mindstick.com/articles/740/xml-document-object-model-dom-in-php  
category: "php"  
tags: ["php"]  
reading_time: 2 minutes  

---

# XML Document Object Model (DOM) in PHP

Document [Object Model](https://www.mindstick.com/forum/158432/what-is-the-document-object-model-dom-and-how-does-it-relate-to-web-development) (DOM) provides a [standard](https://www.mindstick.com/articles/23223/naming-convention-or-coding-standard) set of objects for HTML and XML [documents](https://www.mindstick.com/articles/156931/the-main-types-of-business-documents), and a standard [interface](https://www.mindstick.com/articles/12036/interface-in-c-sharp) for accessing and manipulating them. Since [DOM parser](https://www.mindstick.com/interview/501/what-is-the-difference-between-sax-parser-and-dom-parser) is a tree-based parser so it [transforms](https://www.mindstick.com/forum/33814/how-to-use-transforms-in-css) an XML document into a tree [structure](https://www.mindstick.com/forum/1188/how-to-create-structure-in-c).\

##### DOM is separated into different parts (Core, XML, and HTML) and different levels:

- **Core DOM** - defines a standard set of objects for any [structured](https://yourviews.mindstick.com/view/88498/underbust-corset-styling-guide-for-structured-everyday-looks) document
- **XML DOM** - defines a standard set of objects for XML documents
- **HTML DOM** - defines a standard set of objects for HTML documents

##### Let’s have an example how to load and display xml file with the help of DOM in PHP.

Example: Load and Output [XML file](https://www.mindstick.com/articles/75/how-to-read-and-write-xml-file-through-c-sharp) Using DOM

Below is an XML file.

```
<?xml version="1.0" encoding="ISO-8859-1"?><note>     <to> Amit  </to>     <from> Arun  </from>     <subject> XML Parser</subject>     <message> This is testing message on PHP XML parser</message></note>
```

Now save this file with ‘TestXml.xml’ name.

```
<?php         // Create instance of DOMDocument            $doc =new DOMDocument();         // Load xml filefrom specified loaction         $doc->load("TestXml.xml");        // Display XML file structure         echo $doc->saveXML();  ?>
```

##### Output:

![XML Document Object Model (DOM) in PHP](https://www.mindstick.com/mindstickarticle/647a3dc4-8de6-4200-a805-54c89db41df8/images/4644e628-e50f-4f27-aed3-1b3fdb4469f0.png)

Example: Creating XML Document using with DOMDocument

```
<?php    //CreatingDOMDocument instance with xml version 1.0    $doc = new DOMDocument('1.0');    //  Set output format    $doc->formatOutput = true;      // create root node    $root = $doc->createElement('book');    // add root node as child of domdocument    $root = $doc->appendChild($root);    // Create child node     $title = $doc->createElement('title');    // add child node with root node    $title = $root->appendChild($title);    // Assign child node value    $text = $doc->createTextNode('This is the title');    $text = $title->appendChild($text);    //display number of bytes written and wrote file with name tesAt.xml file name at specified location    echo 'Wrote: ' . $doc->save("tesAt.xml") . ' bytes';   ?>
```

Note: save () method is used to save file at specified location.

##### Output:

![XML Document Object Model (DOM) in PHP](https://www.mindstick.com/mindstickarticle/647a3dc4-8de6-4200-a805-54c89db41df8/images/25a75ce4-a3fd-4f6c-8f04-4e0b198b567e.png)

Now open, above [specified location](https://www.mindstick.com/forum/34118/extjs-failed-to-find-charts-on-the-specified-location) in save () method.

![XML Document Object Model (DOM) in PHP](https://www.mindstick.com/mindstickarticle/647a3dc4-8de6-4200-a805-54c89db41df8/images/5148d6e1-adab-4e55-a0a4-edd52ff01e9a.png)

This is the required ‘tesAt.xml’ file which is creating through the code..

\

---

Original Source: https://www.mindstick.com/articles/740/xml-document-object-model-dom-in-php

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
