Parse XML file in PHP

Hello everyone in this tutorial you are going to learn about parsing the XML file using PHP.

XML File

XML file is defined as an approach for presenting all the information from an unsorted to a sorted way.

In simple terms, XML files arrange the information in a very well-structured format.

Need of XML File

You can use the XML file for data exchange and as a communication channel to transport the data.

Example -> You can edit the unstructured information into a structured format and use it for transmission.

Characteristics features of XML file

  • XML is platform-independent. It means you can create a file in the system regardless of the software or operating system present in it.
  • XML files are both human and machine-readable.
  • You can change the data stored in XML or transport it at any point in time.
  • You can arrange XML files in a hierarchy format. It means you can store elements into other elements.
  • In the XML file, You can call the first node as the root node and all other nodes inside it as the child node.
  • XML is an extensible markup language. It means you can create multiple tags.

Create a XML file

  • In the first step, You can create a root node named  <NameOfCountries> with a start and end node in it.
    <NameOfCountries> 
        
    </NameOfCountries>

     

  • In the next step, create child nodes inside the root node named <Country1>, <Country2> and <Country3>.
    <NameOfCountries> 
        <Country1>
            
        </Country1>
        <Country2>
            
        </Country2>
        <Country3>
            
        </Country3>
    </NameOfCountries>

     

  • In the next step, now insert the country’s name by creating a tag <NameOfCountry> inside the child node.
    <NameOfCountries> 
        <Country1>
            <NameofCountry>India</NameofCountry>
            
        </Country1>
        <Country2>
            <NameofCountry>Australia</NameofCountry>
            
        </Country2>
        <Country3>
            <NameofCountry>USA</NameofCountry>
            
        </Country3>
    </NameOfCountries>

     

  • In the next step, create a child node named <State1> and <State2> into the previous node.
    <NameOfCountries> 
        <Country1>
            <NameofCountry>India</NameofCountry>
            <State1>
                
            </State1>
            <State2>
                
            </State2>
        </Country1>
        <Country2>
            <NameofCountry>Australia</NameofCountry>
            <State1>
                
            </State1>
            <State2>
                
            </State2>
        </Country2>
        <Country3>
            <NameofCountry>USA</NameofCountry>
            <State1>
                
            </State1>
            <State2>
                
            </State2>
        </Country3>
    </NameOfCountries>

     

  • In the next step, insert the tags i.e. <Name> and <Capital> inside the child nodes named <State>.
    <NameOfCountries> 
        <Country1>
            <NameofCountry>India</NameofCountry>
            <State1>
                <Name></Name>
                <Capital></Capital>
            </State1>
            <State2>
                <Name></Name>
                <Capital></Capital>
            </State2>
        </Country1>
        <Country2>
            <NameofCountry>Australia</NameofCountry>
            <State1>
                <Name></Name>
                <Capital></Capital>
            </State1>
            <State2>
                <Name></Name>
                <Capital></Capital>
            </State2>
        </Country2>
        <Country3>
            <NameofCountry>USA</NameofCountry>
            <State1>
                <Name></Name>
                <Capital></Capital>
            </State1>
            <State2>
                <Name></Name>
                <Capital></Capital>
            </State2>
        </Country3>
    </NameOfCountries>

     

  • In the next step, enter the name of the country and its capital inside the tag <Name> and <Capital>
    <NameOfCountries> 
        <Country1>
            <NameofCountry>India</NameofCountry>
            <State1>
                <Name>Chhattisgarh</Name>
                <Capital>Raipur</Capital>
            </State1>
            <State2>
                <Name>Odisha</Name>
                <Capital>Bhubaneswar</Capital>
            </State2>
        </Country1>
        <Country2>
            <NameofCountry>Australia</NameofCountry>
            <State1>
                <Name>New South Wales</Name>
                <Capital>Sydney</Capital>
            </State1>
            <State2>
                <Name>Victoria</Name>
                <Capital>Melbourne</Capital>
            </State2>
        </Country2>
        <Country3>
            <NameofCountry>USA</NameofCountry>
            <State1>
                <Name>Arizona</Name>
                <Capital>Phoenix</Capital>
            </State1>
            <State2>
                <Name>California</Name>
                <Capital>Sacramento</Capital>
            </State2>
        </Country3>
    </NameOfCountries>

PHP File

PHP stands for HyperText PreProcessor, which is an open-source and server-side scripting language.

The term server-side scripting language means that writing and implementing the code all are done on the server side.

Why we need PHP

Due to higher user database and server overload, it leads to severe issues such as ->

  • Breaching issue in data security
  • Poor interactive website
  • Slow running speed of the website

To solve the above issues, users use PHP.

Characteristics features of PHP

  • It fastens the website functions.
  • It provides strong security encryption.
  • It makes websites more interactive for users.
  • It is platform-independent, which means it can work on different operating systems.
  • In PHP only variables are case-sensitive.

Create a PHP File

  • Create a PHP file named as a parser with an extension .php (parser.php).
  • In the next step, create a PHP program with <?php and end with ?>.
    <?php
    
    ?>
    

     

  • In the next step, load the XML file with the data in the PHP by using the following code line
    <?php
    
    //In this load xml file in the file
    $xml = simplexml_load_file('file.xml');
    
    ?>
    

    As $xml is represented as an object in which the entire XML file is loaded.

  • In the next step, access all the child nodes i.e. <Country> from the object $xml by using the foreach loop.

The foreach loop will iterate over all the child nodes named <Country> and will access it from the object $xml.

<?php
//In this load xml file in the file
$xml = simplexml_load_file('file.xml');

//Here, it access the child node named as Country from the object $xml and will iterate over all the child node named as Country.
foreach ($xml->children() as $Country) {

}
?>
  • In the next step, access the name of the country from every child node i.e. <Country> , and print the name as output.
    <?php
    //In this load xml file in the file
    $xml = simplexml_load_file('file.xml');
    
    //Here, it access the child node named as Country from the object $xml and will iterate over all the child node named as Country.
    foreach ($xml->children() as $Country) {
    
        //Now from the object extract the name of the country present in the child node and convert it into string 
        //As here $countryName is the variable where the name of the country will be stored after converting into string
        $countryName = (string) $Country->NameofCountry;
        
        //After the name is extracted from object and converted into string print the name of country.
        //As echo is used as print fucntion in PHP
        echo "Country: $countryName\n";
    }
    ?>

    After extracting the name of the country from the object [ $countryName = (string) $Country->NameofCountry ] is used to convert it into a string.

  • In the next step, create a foreach loop in which you iterate over all the child nodes i.e. <State>.
  • Then afterward, you can access all the <Name> and <Capital> of states from the object $xml convert it into the string, and then print it.
    <?php
    //In this load xml file in the file
    $xml = simplexml_load_file('file.xml');
    
    //Here, it access the child node named as Country from the object $xml and will iterate over all the child node named as Country.
    foreach ($xml->children() as $Country) {
    
        //Now from the object extract the name of the country present in the child node and convert it into string 
        $countryName = (string) $Country->NameofCountry;
    
        //After the name is extracted from object and converted into string print the name of country. 
        echo "Country: $countryName\n";
    
        //Now take all the child node named as state present inside the child node named as country which is present in object i.e. $xml.
        foreach ($Country->children() as $state) {
    
            //Now from the child node named as state extract the name and captial of the state from the xml object and convert it into string.
            $name = (string) $state->Name;
            $capital = (string) $state->Capital;
    
            echo " - State: $name, Capital: $capital\n";
        }
    }
    ?>

Use echo as a print function in PHP.

The above code will show the following output

Ouput
Country: India
- State: , Capital:
- State: Chhattisgarh, Capital: Raipur
- State: Odisha, Capital: Bhubaneswar
Country: Australia
- State: , Capital:
- State: New South Wales, Capital: Sydney
- State: Victoria, Capital: Melbourne
Country: USA
- State: , Capital:
- State: Arizona, Capital: Phoenix
- State: California, Capital: Sacramento

Leave a Comment

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

Scroll to Top