PHP

【PHP】Read and Output XML File Data Using DOMDocument

This article introduces how to use PHP’s DOMDocument to read and output XML file data. It is written for beginners who are new to PHP or XML, with a step-by-step explanation from the basics. If you are interested in how to handle XML data, this article will provide useful information, so please read to the end.

What is DOMDocument?

DOMDocument is part of PHP’s standard library and is a very useful class for handling XML files. Using DOM (Document Object Model), you can manage XML data in a tree structure, allowing access and manipulation of each node (element).

DOMDocument not only reads XML files but can also generate new XML documents, manipulate existing XML data, and output it. Therefore, it plays a crucial role in processing data using XML.

XML File Data Format to Be Read

In this case, we will read an XML file called data.xml with the following format. This file contains several data elements, each holding different pieces of information.

<?xml version="1.0" encoding="UTF-8"?>
<list>
  <data seq="1" name="name01" cat="category01">
    <text>Text01</text>
    <url>https://dad-union.com/</url>
  </data>
  <data seq="2" name="name02" cat="category02">
    <text>Text02</text>
    <url>https://dad-union.com/</url>
  </data>
  <data seq="3" name="name03" cat="category03">
    <text>Text03</text>
    <url>https://dad-union.com/</url>
  </data>
  <data seq="4" name="name04" cat="category04">
    <text>Text04</text>
    <url>https://dad-union.com/</url>
  </data>
  <data seq="5" name="name05" cat="category05">
    <text>Text05</text>
    <url>https://dad-union.com/</url>
  </data>
</list>

 
This XML file contains five data elements, each with information such as seq (sequence number), name, cat (category), text, and url. We will read and process this data using PHP.

PHP Code to Read and Output XML File Data Using DOMDocument

Next, I will introduce the PHP code to read and output the contents of the XML file. To run this code, you need to have the php-dom module installed on your server. Usually, it is included in the php-xml module, so there is often no need for special installation.

<?php
$doc = new DOMDocument();
$doc->formatOutput = TRUE;
$doc->preserveWhiteSpace=FALSE;
$doc->load('data.xml');	// Read XML file
$data = $doc->documentElement;
$list = $doc->getElementsByTagName('data');

// Loop through XML file and read tags and attributes
for($j=0; $j<$list->length; $j++){
		$seq = urldecode($list->item($j)->getAttribute("seq"));
		$name = $list->item($j)->getAttribute("name");
		$cat = $list->item($j)->getAttribute("cat");
		$text_list = $list->item($j)->getElementsByTagName('text');
		$text = urldecode(nl2br($text_list->item(0)->nodeValue));
		$url_list = $list->item($j)->getElementsByTagName('url');
		$url = urldecode($url_list->item(0)->nodeValue);

		$str_work = $seq."|".$name."|".$cat."|".$text."|".$url."<br>";
		
		print $str_work;
}
?>

 
This code uses the DOMDocument class to load the data.xml file and access each element in the XML. By using a loop, it retrieves the sequence number, name, category, text, and URL for each data element, formats them into a single line of text, and outputs it.

When you run this sample code, the data will be output in the following format:

1|name01|category01|Text01|https://dad-union.com/
2|name02|category02|Text02|https://dad-union.com/
3|name03|category03|Text03|https://dad-union.com/
4|name04|category04|Text04|https://dad-union.com/
5|name05|category05|Text05|https://dad-union.com/

 
In this way, you can easily handle and output XML data with PHP.

Demo Page to Read and Output XML File Data Using PHP

For those who want to see the actual result of the code introduced in this article, I have prepared a demo page. You can access it from the link below.

Demo page to read and output XML file data using PHP

In the demo page, you can verify that the XML data is being read and output correctly. You can also try running the code in your environment to see if you get the same results.

Conclusion

In this article, I explained how to read, manipulate, and output XML file data using PHP’s DOMDocument class. XML is widely used for data exchange and storage, so understanding how to handle it is very useful for engineers.

This method is particularly effective when dealing with simple XML files, but if the data is large or the structure is complex, it may be more efficient to use a database. However, it is important to first understand the basic XML operations and be able to apply them.

I hope this article has been helpful in processing XML files. I will continue to provide useful information for a wide range of engineers through my technical blog, so please keep following.

 
*Please use this information at your own risk.
*Do not reuse the Google Analytics tag from the demo page’s head section.