Facebook Twitter Instagram
    TecAdmin
    • Home
    • FeedBack
    • Submit Article
    • About Us
    Facebook Twitter Instagram
    TecAdmin
    You are at:Home»General Articles»How To Read XML file in PHP (SimpleXML)

    How To Read XML file in PHP (SimpleXML)

    By RahulAugust 26, 20191 Min Read

    SimpleXML is a PHP extension introduced with PHP 5. It allows users to easily handle XML data in PHP. SimpleXML converts any XML data to an object which can be easily processed with normal property selectors and array iterators.

    Advertisement

    You must have installed php-simplexml extension on your system to use examples of this tutorial.

    A Sample XML File

    Here is a sample XML file used for this tutorial. The XML filename is employees.xml which you will see in further examples of this tutorial.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    <?xml version="1.0"?>
    <company>
    <employee>
    <firstname>Tom</firstname>
    <lastname>Cruise</lastname>
    <designation>MD</designation>
    <salary>500000</salary>
    </employee>
    <employee>
    <firstname>Tyler</firstname>
    <lastname>Horne</lastname>
    <designation>CEO</designation>
    <salary>250000</salary>
    </employee>
    </company>

    Read Specific XML Elements

    Use simplexml_load_file function to load external XML file in your PHP program and create an object. After that, you can access any element from the XML by this object as follows.

    1
    2
    3
    4
    5
    <?php
      $xmldata = simplexml_load_file("employees.xml") or die("Failed to load");
      echo $xmldata->employee[0]->firstname . "<\n>";
      echo $xmldata->employee[1]->firstname;
    ?>

    Output:

    Tom
    Tyler
    

    If the XML file is available on the remote server, you can use HTTP URL for the XML file as followings:

    1
    2
    3
    4
    5
    <?php
      $xmldata = simplexml_load_file("https://tecadmin.net/employees.xml") or die("Failed to load");
      echo $xmldata->employee[0]->firstname . "<\n>";
      echo $xmldata->employee[1]->firstname;
    ?>

    Read XML Elements In A Loop

    In this example, we use the foreach method to iterate through the entire XML file and read elements from XML. Foreach loop access all children of an object.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    <?php
    $xmldata = simplexml_load_file("employees.xml") or die("Failed to load");
    foreach($xmldata->children() as $empl) {        
    echo $empl->firstname . ", ";    
    echo $empl->lastname . ", ";    
    echo $empl->designation . ", ";        
    echo $empl->salary . "<\n>";
    }
    ?>

    Output:

    Tom, Cruise, MD, 500000
    Tyler, Horne, CEO, 250000
    

    PHP simplexml xml
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email WhatsApp

    Related Posts

    How to List Manually Installed Packages in Ubuntu & Debian

    10 Bash Tricks Every Developer Should Know

    How to Validate Email Address in JavaScript

    View 13 Comments

    13 Comments

    1. anggie on May 19, 2021 6:29 am

      how to read xml from Report Windows Log ?

      like this:

      Please help

      Reply
    2. anggie on May 18, 2021 9:33 am

      how to read xml from Report Windows Log ?

      like this:

      4663
      ……..

      Please help

      Reply
    3. Victor on April 20, 2021 2:06 pm

      Thanks for share it Rahul.

      Reply
    4. Zahid Hasan on March 11, 2021 5:25 pm

      hey,
      I store xml data in MySQL database. Now, I want to retrieve all the that data with PHP and show in a website.
      But it returns nothing

      Reply
    5. Azaleo on August 25, 2019 9:21 am

      Hello,

      How to retrieve the data if employees.xml is from another website?

      Reply
      • Rahul on August 26, 2019 4:34 am

        You can simply put http url. I have also added an example in this tutorial.

        Reply
    6. Maiara on June 14, 2019 11:36 pm

      Hi, Google article, i stay practice php and i so like this language. Thanks 🙂

      Reply
    7. Pedro on June 12, 2019 5:20 pm

      Thanks for this post, helped me thanks brow!!

      Reply
    8. mark on June 8, 2019 8:11 am

      if $xmldata containes a single quote how can that be handled ?
      I want to use something like str_replace(“‘”,” “,$xmldata) but that wont work

      Reply
    9. Roberto on June 1, 2019 6:04 pm

      Hi thank you for solution, for nested xml?
      Example:

      –
      63856
      –
      –
      NWBASO4ML-0008

      –
      NWBASO4ML-0005
      Base Solution N 4 ml in acqua

      Reply
    10. jinal on May 23, 2019 10:38 am

      Hey my xml markup lost.

      Reply
    11. Taylor on April 20, 2019 3:24 pm

      There is an error on your sample code. You forget the s on implexml_load_file in your loop example. Please correct it.

      Reply
      • Rahul on April 21, 2019 2:07 am

        Thanks Taylor, I have corrected the typo.

        Reply

    Leave A Reply Cancel Reply

    Advertisement
    Recent Posts
    • How to List Manually Installed Packages in Ubuntu & Debian
    • 10 Bash Tricks Every Developer Should Know
    • How to Validate Email Address in JavaScript
    • Firewalld: Common Firewall Rules and Commands
    • 12 Apk Commands in Alpine Linux Package Management
    Facebook Twitter Instagram Pinterest
    © 2023 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.