Parse XML in PHP

In this tutorial, you will be going through the steps of parsing a XML file into a PHP file. This is a very simple guide which anyone can understand and parse XML into PHP. Let’s dive into the contents:

What is XML?

Extensive Markup Language (XML) lets you define and store data in shareable manner. XML supports information exchange between computer systems such as websites, databases and third party applications. Predefined rules makes it easier to transmit data as XML files over any network because the recipient can use those rules to read the data accurately and efficiently.

Characteristics of XML

  • XML is structured format which means that we can define exactly how the data is to be arranged organised and expressed within the file.
  • XML can easily describe hierarchical data and the relationships between the data.
  • Xml can be validated which means we can provide a second xml file.
  • Xml is a strongly typed-format which means the schema definition file specifies the data type of each element.

Creating the XML File

Create a file by giving any name but the extension must be (.xml) which will create a XML file. Here’s a example showing the structure of university with different colleges.:

<university>
    <college>
        <name>IEM</name>
        <location>Kolkata</location>
        <department>BCA, B.Tech</department>
    </college>
    <college>
        <name>SRM University</name>
        <location>Tamil Nadu</location>
        <department>BCA, MCA, B.Tech</department>
    </college>
    <college>
        <name>BSTM</name>
        <location>Chinsura</location>
        <department>BCA, BBA, BHM</department>
    </college>
</university>

Explanation:

<university>: This is the root element that contains all the college information.
<college>: This is a child element is repeated for each college in the university. It contains details about each college.
<name>: This tag contains name of the college (IEM, SRM, BSTM).
<location>: The tag contains location of the college (Kolkata, Tamil Nadu, Chinsura).
<department>: This tag contains the department/courses offered by the colleges (BCA, B.Tech, M.Tech, MCA).

What is PHP?

PHP stands for Hypertext Preprocessor. PHP is an open-source server-side scripting language that many developers use for web development. It is also a general-purpose language that you can use to make lots of projects, including Graphical User Interface (GUI).

Parsing XML in PHP File

Create a file by giving any name but the extension must be (.php) which will create a PHP file. Here’s a example showing the structure of PHP code.

<?php
$val= simplexml_load_file("data.xml");
$a=$val->college;
foreach($a as $m){
    foreach($m->location as $n){
        echo "$n"."<br>";
    }
}
?>

Explanation:

<?php…?>: It indicates the starting and ending of a PHP file.
$val= simplexml_load_file(“data.xml”);: This line loads the XML file named ‘data.xml’ and parses it into a ‘SimpleXMLElement’ object stored in ‘$val’.
$n= $val->college;: This stores the ‘<college>’ elements from the XML into a variable ‘$a’.
foreach($a as $m){ : This ‘<foreach>’ loop iterates over each ‘<college>’ element if there is only one ‘<college>’ element it will process that single item.
foreach($m->location as $n){ : This inner ‘<foreach>’ loop iterates over each ‘<location>’ element within the current ‘<college>’ element.
echo “$n”.”<br>”;: This ‘<echo>’ prints its value followed by a line break ‘<br>’.

Output:

Conclusion

This PHP code loads an xml file that extracts the ‘<college>’ element using ‘SimpleXMLElement’ It then iterates through the nested ‘<location>’ element within each ‘<college>’ displaying their value with line breaks. This process effectively handles and presents hierarchical XML data.

Have a Happy and a Great Coding!

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top