PHP Introduction | Using XML Data | XML and SimpleXML
PHP provides several features for processing XML. Before PHP 5, XML was processed by calling various functions, but from PHP 5, an object-based processing method was added. Here, let us use one of those features, called SimpleXML, to manipulate XML.
As the name suggests, SimpleXML is a very simple feature for using XML. It reads data from an XML file or text and creates an instance of a class called SimpleXMLElement. You process XML data by calling methods and other features on this object.
Create an object from an XML file.
$variable = simplexml_load_file(file specification);
Create an object from a text value.
$variable = simplexml_load_string(text);
These functions create an object by specifying a file, such as a file name or file path, or XML text as the first argument. These functions actually have more arguments, but if you simply want to create an XML object from the specified data, one argument is enough.
You can also create a SimpleXMLElement object with new, instead of calling these functions. This also has several arguments, but the method using three arguments is as follows.
Create a SimpleXMLElement
$variable = new SimpleXMLElement(data, options, boolean);
The first argument is XML data. This can be XML text or the URL of an XML file. The second argument specifies option values; for now, specify 0. 0 means there are no options.
The boolean third argument specifies whether the first argument is a data file. If XML text is specified as the first argument, use false, or omit it. If a file URL is specified, set it to true. When retrieving data from a file, object creation fails unless this third argument is set to true, so it must be specified.
Now we can create a SimpleXMLElement object for processing XML data.
Creating an XML File
As an example of manipulating XML, let us create simple XML data. Write the following sample code.
data.xml
<?xml version="1.0" encoding="UTF-8"?>
<datas>
<data>
<name>Windows</name>
<version>9</version>
<price>99</price>
</data>
<data>
<name>Mac</name>
<version>15</version>
<price>99999</price>
</data>
<data>
<name>Linux</name>
<version>123.4567</version>
<price>0</price>
</data>
</datas>
Place this as a file named data.xml in the same location as the PHP file, such as the index.php file you will create.
Getting Data from SimpleXMLElement
SimpleXMLElement provides various methods. You can use those methods to obtain the information you need from XML.
The easiest way to get the necessary information from SimpleXMLElement is to specify the corresponding node as a property.
$variable = $object->node;
Write it like this. Here, a “node” can be thought of as each individual tag written in XML data. It is created as a SimpleXMLElement object, and each tag becomes an object. The object representing each individual element is also called a node.
In XML, multiple nodes with the same name may exist at the same level. In such cases, the objects representing those nodes are obtained as an array. Therefore, retrieve the specified node and process it in a loop to handle multiple nodes.
Let us make an actual usage example.
<?php
$data = "";
$xml = new SimpleXMLElement("data.xml",0,true);
$data_arr = $xml->data;
foreach ($data_arr as $row) {
$data .= "<tr>";
$data .= "<td>" . $row->name . "</td>";
$data .= "<td>" . $row->version . "</td>";
$data .= "<td>" . $row->price . "</td>";
$data .= "</tr>";
}
?>
<!DOCTYPE html>
<html lang="ko">
<head>
<meta http-equiv="Content-Type"
content="text/html; charset=UTF-8" />
<title>sample page</title>
<style>
h1 { font-size:14pt;
padding:5px;
background-color:#AAFFFF; }
table tr td {
padding:5px;
background-color:#DDFFCC; }
</style>
</head>
<body>
<h1>Hello PHP!</h1>
<table>
<tr><th>Name</th><th>Version</th><th>Price</th></tr>
<?php echo $data; ?>
</table>
</body>
</html>
Here, the data.xml from above is loaded and displayed. In this data.xml file, the root has a <datas> tag, and data is stored inside <data> tags. This data stores values such as product name, version, and price.
Here, the file is read in the following way.
$xml = new SimpleXMLElement("data.xml", 0, true);
This creates the SimpleXMLElement object variable $xml from data.xml, and then retrieves all <data> tag nodes from it.
$data_arr = $xml->data;
As shown above, specify the node name with $xml->data to retrieve the nodes. It is very simple. After that, each node is processed in a loop.
foreach ($data_arr as $row) {...... omitted
This retrieves objects one by one from the array of node objects into $row, and then uses $row to take out each element’s value and build text.
$data .= "<td>" . $row->name . "</td>";
$data .= "<td>" . $row->version . "</td>";
$data .= "<td>" . $row->price . "</td>";
Once you understand the basic usage, you can see that using XML is very simple.