XML document is used to store a small amount of data, and sometimes it is required to read the particular content of XML document based on the path value using PHP script. xpath() function is used to parse the content of an XML document. This function can be used by using simplexml_load_file() function or by creating the object of SimpleXMLElement class. The xpath() function can be used to read the particular XML node values shown in this tutorial.

Syntax:

The syntax of the xpath() function is given below.

array xpath(string $path)

This function has one argument that takes a path value, and if the path exists in any node of the XML document, then the value of the node will be returned as an array. Different uses of this function have explained in the next part of this tutorial.

Create XML document:

Create an XML file named products.xml with the following content on the location where the PHP file will be created to parse this file.

<?xml version=“1.0”?>

<PRODUCTS>

<PRODUCT category=“Monitor”>

<ID>MN56345</ID>

<BRAND>DELL</BRAND>

<NAME>15 inches Dell Monitor</NAME>

<PRICE>700</PRICE>

</PRODUCT>

<PRODUCT category=“HDD”>

<ID>HD34278</ID>

<BRAND>SAMSUNG</BRAND>

<NAME>1 TB Samsung HDD</NAME>

<PRICE>520</PRICE>

</PRODUCT>

<PRODUCT category=“Mouse”>

<ID>MS67457</ID>

<BRAND>LOGITECH</BRAND>

<NAME>Logitech Wireless Mouse</NAME>

<PRICE>100</PRICE>

</PRODUCT>

<PRODUCT category=“Monitor”>

<ID>MN76453</ID>

<BRAND>HP</BRAND>

<NAME>14 inches HP Monitor</NAME>

<PRICE>750</PRICE>

</PRODUCT>

</PRODUCTS>

Use of simplexml_load_file() function:

The xpath() function with the object created by the simplexml_load_file() function has shown in this part of this tutorial.

Example-1: Read the particular XML node values

The following example shows how to read the content of the particular node values from the XML document by defining the xpath() function path. The object variable, $xml is created to read the specific node values of the products.xml file. ‘/PRODUCTS/PRODUCT/BRAND’ is used in the xpath() to read all BRAND node values. ‘/PRODUCTS/PRODUCT’ is used in the xpath() to read all child node values of PRODUCT nodes. foreach loop is used to print the values of NAME and PRICE nodes.

<?php

//Create object to read the XML file

$xml = simplexml_load_file(‘products.xml’);

//Search all BRAND node values

$brand = $xml->xpath(‘/PRODUCTS/PRODUCT/BRAND’);

//Print the array values

echo

The list of brand names are:

;

foreach($brand as $name) {

echo $name
;

}

//Search all PRODUCT node values

$products = $xml->xpath(‘/PRODUCTS/PRODUCT’);

echo

The list of product name and price:

;

echo

;

echo

;

//Print the array values

foreach($products as $product) {

echo

;

}

echo

Name Price
$product->NAME $$product->PRICE

;

?>

Output:

The following output will appear after running the script from the server.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/01/echo/u1.png6003c2bb52e90.jpg" data-lazy- height="506" src="data:image/svg xml,” width=”1088″>

Example-2: Read the particular XML node values based on condition

The following example shows how to read the particular node values based on the condition using xpath() function. The path value, ‘/PRODUCTS/PRODUCT[PRICE > 600]’, will search the values of all child nodes of PRODUCT node where the value of PRICE node is more than 600. foreach loop is used to print the values of NAME and PRICE nodes.

<?php

//Create object to read the XML file

$xml = simplexml_load_file(‘products.xml’);

//Search the products where the price value is more than 600

$products = $xml->xpath(‘/PRODUCTS/PRODUCT[PRICE > 600]’);

echo

The list of product name and price where the price is more than 600:

;

echo

;

echo

;

//Print the array values

foreach($products as $product) {

echo

;

}

echo

Name Price
$product->NAME $$product->PRICE

;

?>

Output:

The following output will appear after running the script from the server.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/01/echo/u2.png6003c2bbe065a.jpg" data-lazy- height="244" src="data:image/svg xml,” width=”1088″>

Example-3: Read the particular XML node values based on attribute

The following example shows how to read the particular node values based on the attribute values of the XML document’s specific node by using the xpath() function. The path value, ‘/PRODUCTS/PRODUCT[@category=”Monitor”]’ will search the values of all child nodes of the PRODUCT node, where the category attribute’s value is Monitor. foreach loop is used to print the values of BRAND, NAME, and PRICE nodes.

<?php

//Create object to read the XML file

$xml = simplexml_load_file(‘products.xml’);

//Search the products where the çategory attribute value is ‘Monitor’

$products = $xml->xpath(‘/PRODUCTS/PRODUCT[@category=”Monitor”]’);

echo

The list of product brand,name and price based on category(Monitor):

;

echo

;

echo

;

//Print the array values

foreach($products as $product) {

echo

;

}

echo

Brand Name Price
$product->BRAND $product->NAME $$product->PRICE

;

?>

Output:

The following output will appear after running the script from the server.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/01/echo/u3.png6003c2bc93391.jpg" data-lazy- height="230" src="data:image/svg xml,” width=”1087″>

Use of SimpleXMLElement class:

The uses of xpath() function by creating the SimpleXMLElement class object have shown in this part of this tutorial.

Example-4: Use of xpath() by defining XML content in a variable

The following example shows how to read the XML document’s node values declared in a variable instead of the file. XML content is stored in the $xml_data variable. “/customers/customer” is used as the argument value of xpath() function to read all values of the child nodes of the customer node. Next, a for each loop is used to print the values of the name node. “/customers/customer[@department=’HR’]” is used as the argument value of another xpath() function to read all values of the child nodes of customer node where the value of department attribute is HR. Next, a for each loop is used to print the values of the email node.

<?php

$xml_data = <<<XML








Md. Mahbub


[email protected]








Farhana Zaman


[email protected]




XML

;

//Define object to read the XML data

$xml = new SimpleXMLElement($xml_data);

//Define path to read all customers data

$customers = $xml->xpath(“https://linuxhint.com/customers/customer”);

//Print the name of the customers

echo

The list of customer names:

;

foreach($customers as $customer) {


    echo $customer->name
;

}

//Define path to read all customer data of HR department

$customers = $xml->xpath(“https://linuxhint.com/customers/customer[@department=’HR’]”);

//Print the email of the customers

echo

The customer’s email of HR department:

;

foreach($customers as $customer) {


    echo $customer->email
;

}

?>

Output:

The following output will appear after running the script from the server.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/01/echo/u4.png6003c2bd2bc00.jpg" data-lazy- height="301" src="data:image/svg xml,” width=”1088″>

Conclusion:

Two different ways of using the xpath() function to read the XML document’s node values based on the specific path or the path with the condition or the path with attribute value have been explained in this tutorial by using multiple examples.

About the author

<img alt="Fahmida Yesmin" data-lazy-src="https://kirelos.com/wp-content/uploads/2021/01/echo/channel-logo-150×150.jpg6003c2bda1872.jpg" height="112" src="data:image/svg xml,” width=”112″>

Fahmida Yesmin

I am a trainer of web programming courses. I like to write article or tutorial on various IT topics. I have a YouTube channel where many types of tutorials based on Ubuntu, Windows, Word, Excel, WordPress, Magento, Laravel etc. are published: Tutorials4u Help.