Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

PHP XML DOM


May 11, 2021 PHP


Table of contents


PHP XML DOM

For operating XML type files, PHP provides a set of built-in DOM objects for processing. F or XML operations, functions in doM objects can be used from creation, addition to modification, and deletion.

The built-in DOM parser makes it possible to work with XML documents in PHP.


What is DOM?

W3C DOM provides a standard set of objects for HTML and XML documents, as well as a standard interface for accessing and operating those documents.

W3C DOM is divided into different sections (Core, XML and HTML) and different levels (DOM Level 1/2/3):

Core DOM - Define a standard set of objects for any structured document
XML DOM - Define a standard set of objects for XML documents
* HTML DOM - Defines a standard set of objects for HTML documents

To learn more about XML DOM, visit our XML DOM tutorial.


XML resolution

To read and update - Create and process - an XML document, you need an XML parser.

There are two basic XML parser types:

  • Tree-based parsers: This parser converts XML documents into tree structures. It analyzes the entire document and provides access to elements in the tree, such as the Document Object Model (DOM).
  • Time-based parser: Treats an XML document as a series of events. When a specific event occurs, the parser calls a function to handle it.

The DOM parser is a tree-based parser.

Take a look at the following fragments of the XML documentation:

<?xml version="1.0" encoding="ISO-8859-1"?>
<from>Jani</from>

XML DOM treats the above XML as a tree structure:

  • Level 1: XML documentation
  • Level 2: Root Elements: slt;from
  • Level 3: Text Element: "Jani"

Installation

DoM XML parser functions are part of the PHP core. You can use these functions without installation.


XML file

The following XML files will be applied in our example:

<?xml version="1.0" encoding="ISO-8859-1"?>
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>


Load and output XML

We need to initialize the XML parser, load the XML, and output it:

Instance

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

 print $xmlDoc->saveXML();
 ?> 

The above code will output:

ToveJaniReminder Don't forget me this weekend!

If you look at the source code in your browser window, you'll see the following HTML:

<?xml version="1.0" encoding="ISO-8859-1"?>
 <note>
 <to>Tove</to>
 <from>Jani</from>
 <heading>Reminder</heading>
 <body>Don't forget me this weekend!</body>
 </note> 

The instance above creates a DOMDocument-Object and loads the XML .xml "note" into the document object.

The saveXML() function puts the internal XML document into a string so that we can output it.


Traverse XML

We're going to initialize the XML parser, load the XML, and traverse all the elements of the element:

Instance

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

 $x = $xmlDoc->documentElement;
 foreach ($x->childNodes AS $item)
 {
 print $item->nodeName . " = " . $item->nodeValue . "<br>";
 }
 ?> 

The above code will output:

#text =
to = Tove
#text =
from = Jani
#text =
heading = Reminder
#text =
body = Don't forget me this weekend!
#text =

In the example above, you see an empty text node between each element.

When XML is generated, it usually contains white space between nodes. XML DOM parsers treat them as ordinary elements, and if you don't pay attention to them, sometimes there are problems.


To learn more about XML DOM, visit our XML DOM tutorial.

In the next section, we'll start with PHP SimpleXML.