Import XML File in PHP

By CampCodes Administrator

Published on:

import xml file in php

In this tutorial, we will create a Simple Import XML File using PHP. This code can import the XML file to a data table when the user uploads the XML file. The code uses simplexml_load_file to load a block of data from the uploaded XML file to view it as a readable table format. This is a user-friendly kind of program feel free to modify it.

We will be using XML as a markup language that utilizes in PHP as HTML data. It is designed to store and transport data that can be manipulated within the local server.

Getting Started:

First, you have to download & install XAMPP or any local server that runs PHP scripts. Here’s the link for the XAMPP server https://www.apachefriends.org/index.html.

And, this is the link for the bootstrap that I used for the layout design https://getbootstrap.com/.

Creating The Interface

This is where we will create a simple form for our application. To create a copy of the form and write it into your text editor, then save it as index.php.

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1" />
        <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
    </head>
<body>
    <nav class="navbar navbar-default">
        <div class="container-fluid">
            <a class="navbar-brand" href = "https://sourcecodester.com">Sourcecodester</a>
        </div>
    </nav>
    <div class="col-md-3"></div>
    <div class="col-md-6 well">
        <h3 class="text-primary">PHP - Simple Import XML File</h3>
        <hr style="border-top:1px dotted #000;" />
        <div class="col-md-4">
            <form method="POST" action="" enctype="multipart/form-data">
                <h5>Upload XML here</h5>
                <input type="file" name="file"/>
                <br />
                <center><button name="import" class="btn btn-primary">Import</button></center>
            </form>
        </div>
        <div class="col-md-8">
            <?php include'import.php'?>
        </div>
    </div>
</body>
</html>

Creating the Main Function

This code contains the main function of the application. This code will upload and display a xml data when the button is clicked. To make this just copy and write these block of codes below inside the text editor, then save it as import.php.

<?php
    if(ISSET($_POST['import'])){
        $file_name=$_FILES['file']['name'];
        $exp=explode('.', $file_name);
        $name=end($exp);
 
        if($name=="xml"){
?>
    <table class="table table-bordered" >	
        <thead class="alert-info">
            <tr>
                <th>Firstname</th>
                <th>Lastname</th>
                <th>Address</th>
            </tr>
        </thead>
 
        <tbody>
 
            <?php
                $xml = simplexml_load_file(''.$file_name);
                foreach($xml->member as $member){
                    echo '
                        <tr>
                            <td>'.$member->firstname.'</td>
                            <td>'.$member->lastname.'</td>
                            <td>'.$member->address.'</td>
                        </tr>
                    ';
                }
            ?>
        </tbody>
    </table>
<?php
        }else{
            echo"<script>alert('Please upload xml file only')</script>";
        }
    }else{
?>
    <table class="table table-bordered" >	
        <thead class="alert-info">
            <tr>
                <th>Firstname</th>
                <th>Lastname</th>
                <th>Address</th>
            </tr>
        </thead>
 
        <tbody>
            <td></td>
            <td></td>
            <td></td>
        </tbody>
    </table>
<?php
    }
?>
convert mysql to xml using php create xml file in php simplexml display xml in html echo xml in php element examples generate xml file using php mysql generate xml from mysql database how can i download xml file in php? how parse xml in php? how to create dynamic xml file in php how to create xml file in php example how to display xml data in php how to get attribute value in xml using php how to get data from xml file in php how to parse xml in php how to store data in xml file using php how to write xml file in php html to xml php import xml file in php libxml_nocdata load xml file php parse xml with php php array to xml php array to xml with attributes php convert xml to array with attributes php convert xml to object php create xml php create xml file and save php create xml string php curl xml response to array php deserialize xml php display xml php display xml in browser php domdocument php echo xml php echo xml string php edit xml file php generate xml php generate xml from database php get xml from url php load xml php load xml file php load xml file into array php load xml from string php load xml from url php loadxml php new simplexmlelement php output xml php output xml file php parse simplexmlelement object php parse xml php parse xml example php parse xml file php parse xml from url php parse xml response php parse xml string php parse xml to array php parse xml with multiple namespaces php parsing xml data php parsing xml file php print xml data php read xml php read xml file php read xml file example php read xml file into array php read xml from url php return xml response php rss to json php search xml php simple xml php simplexml php simplexml attributes php simplexml example php simplexml foreach php simplexml get attribute php simplexml get attribute by name php simplexml load file php simplexml to array php simplexml to array with attributes php simplexml tutorial php simplexml_load_file php simplexml_load_file example php simplexml_load_string php simplexmlelement php simplexmlelement example php simplexmlelement get value php simplexmlelement to string php string to xml php to xml php to xml converter php tutorials php validate xml string php with xml php write xml php write xml file php xml php xml api php xml api call example php xml class php xml editor php xml encode php xml encode array php xml extension php xml file to array php xml functions php xml get attribute php xml get element by attribute value php xml library php xml manipulation php xml parse php xml parser php xml parser example php xml parser library php xml parser tutorial php xml parsers php xml parsing example php xml reader php xml request php xml string to array php xml to array php xml to array with attributes php xml to associative array php xml to json php xml to json with attributes php xml to object php xml to object mapping php xml to string php xml tree php xmlreader php xmlreader expand php xmlreader nodetype php xmlwriter php5 parse xml php5 xml parsing read write xml php read xml file in php read xml in php read xml php read xml using php reading xml file in php reading xml file using php sample xml file simple xml simple xml php simplexml get attribute simplexml import sax simplexml load string simplexml php simplexml_load_file simplexml_load_file error simplexml_load_file error handling simplexml_load_file php simplexml_load_file php 7 simplexml_load_file url simplexml_load_string cdata simplexml_load_string error simplexml_load_string namespace simplexml_load_string php simplexml_load_string returns empty simplexmlelement example simplexmlelement get value simplexmlelement object to associative array simplexmlelement to json simplexmlelement to xml what is simple xml extension? what is xml used for which of the following method can be used to parse an xml document using php? xml api in php xml editor php xml example xml export php xml file xml file example xml foreach xml in php xml parser class php xml parser in php example xml parser php xml to array php xml to html php xml to string php xml2array php xmlreader php xmlreader php example

Leave a Comment