Skip to content
Related Articles
Get the best out of our app
GeeksforGeeks App
Open App

Related Articles

How to display XML data in web page using PHP ?

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

In this article, we are going to display data present in an XML file on a web page using PHP through the XAMPP server. PHP is a server-side scripting language that is mainly for processing web data. The XML stands for an extensible markup language.


  • XAMPP server



Approach: We are going to use mainly two functions in our PHP code. The simplexml_load_file() function is used to convert an XML document to an object.

  • simplexml_load_file
    simplexml_load_file(name of XML file)
  • children(): The children() function finds the children of a specified node.


Steps to execute:

  • Step 1: Start XAMPP server.
  • Open notepad and type the following codes in xml_data.xml and code.php formats The xml_data.xml: Consider student XML data as an example.


    <?xml version = "1.0" encoding = "utf-8"?>
       <department category = "IT">
          <subjects lang = "en">java</subjects>
          <name>G.Sravan Kumar</name>
       <department category = "CSE">
          <subjects lang = "en">Python</subjects>
          <name>B. Naga sudheer</name>
       <department category = "IT">
          <subjects lang = "en">sql</subjects>

  • Step 3:The following is the code for code.php file.


    // Start php code
    // Load xml file into xml_data variable
    $xml_data = simplexml_load_file("xml_data.xml") or 
    die("Error: Object Creation failure");
    // Use foreach loop to display data and for sub elements access,
    // We will use children() function
    foreach ($xml_data->children() as $data)
        //display each sub element in xml file
        echo "Subject name : ", $data->subjects . "<br> ";
        echo "Student name : ", $data->name . "<br> ";
        echo "Student age : ", $data->age . "<br> ";
        echo "Student marks : ", $data->marks . "<br>";
        echo "Student address : ", $data->address . "<br>";
        echo "------------------------------------";
        echo "<br>";

  • Step 4: Save these two files in xampp/htdocs/geek folder. The developer can use any other folder instead of geek folder.

Output: Open your browser and type localhost/geek/code.php to see the output.

XML data 

My Personal Notes arrow_drop_up
Last Updated : 31 Mar, 2021
Like Article
Save Article
Similar Reads
Related Tutorials