• We just launched and are currently in beta. Join us as we build and grow the community.

PHP - Simple Load XML Data

KMART240777

Backend System Visionary
K Rep
0
0
0
Rep
0
K Vouches
0
0
0
Vouches
0
Posts
95
Likes
101
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 200 XP
In this tutorial we will create a Simple Load XML Data using PHP. This code can display XML file into a readable HTML table. The code use simplexml_load_file to call a block of data within the xml file when the user click the button. This is a user-friendly kind of program feel free to modify it.

We will be using XML as a markup language that utilize in php as a 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 run PHP scripts. Here's the link for 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 the forms simply copy and write it into your text editor, then save it as index.php.
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1" />
  5. <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
  6. </head>
  7. <body>
  8. <nav class = "navbar navbar-default">
  9. <div class = "container-fluid">
  10. <a class = "navbar-brand" href = "https://sourcecodester.com">Sourcecodester</a>
  11. </div>
  12. </nav>
  13. <div class = "col-md-3"></div>
  14. <div class = "col-md-6 well">
  15. <h3 class = "text-primary">PHP - Simple Load XML Data</h3>
  16. <hr style = "border-top:1px dotted #000;" />
  17. <form method="POST">
  18. <button name="load" class="btn btn-primary"><span class="glyphicon glyphicon-book"></span> Load XML</button>
  19. </form>
  20. <br /><br />
  21. <?php

    include

    'load_xml.php'

    ?>
  22. </div>
  23. </body>
  24. </html>

Creating the Main Function

This code contains the main function of the application. This code will load 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 shown below.
data.xml

xml version="1.0"?

John Smith

Male

I

B

Claire

Temple

I

A

load_xml.php
  1. <?php
  2. if

    (

    ISSET

    (

    $_POST

    [

    'load'

    ]

    )

    )

    {
  3. ?>

  4. <table class="table table-bordered" >
  5. <thead class="alert-info">
  6. <tr>
  7. <th>Name</th>
  8. <th>Gender</th>
  9. <th>Year</th>
  10. <th>Section</th>
  11. </tr>
  12. </thead>

  13. <tbody>

  14. <?php
  15. $xml

    =

    simplexml_load_file

    (

    'data.xml'

    )

    ;
  16. foreach

    (

    $xml

    ->

    student

    as

    $student

    )

    {
  17. echo

    '
  18. <tr>
  19. <td>'

    .

    $student

    ->

    name

    .

    '</td>
  20. <td>'

    .

    $student

    ->

    gender

    .

    '</td>
  21. <td>'

    .

    $student

    ->

    year

    .

    '</td>
  22. <td>'

    .

    $student

    ->

    section

    .

    '</td>
  23. </tr>
  24. '

    ;
  25. }
  26. ?>
  27. </tbody>
  28. </table>
  29. <?php
  30. }

    else

    {
  31. ?>
  32. <table class="table table-bordered">
  33. <thead class="alert-info">
  34. <tr>
  35. <th>Name</th>
  36. <th>Gender</th>
  37. <th>Year</th>
  38. <th>Section</th>
  39. </tr>
  40. </thead>

  41. <tbody>

  42. </tbody>
  43. </table>
  44. <?php
  45. }
  46. ?>

There you have it we successfully created Simple Load XML Data using PHP. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!


Download
You must upgrade your account or reply in the thread to view the hidden content.
 

452,496

333,651

333,659

Top