How to generate an XML file dynamically using PHP?

A file can be generated using PHP from the database and it can be done by the static or dynamic method in PHP. Static methods can be called directly – without creating an instance of a class. Here we are going to discuss how to create an XML file dynamically.

  • The first thing we need to do is fetch the data from the database. For that, we need to write a select query that will fetch all the details from the table.
    $result=mysql_query("Select * from Table_name");
  • Now we need to create an XML file using DOMDocument in which we will specify the version. DOMDocument represents an entire HTML or XML document, serves as the root of the document tree.
    $xml = new DOMDocument("1.0");
  • Now, we will create elements of the XML document. It will create new element node using createElement() function. It creates a new instance of class DOMElement. This node will not show up in the document unless it is inserted with (e.g.) DOMNode::appendChild().
  • Till now we have created an XML file. So to display this we are going to use an echo tag that shows the data from a file in XML format. To save the XML file we will use save command.
    echo "".$xml->saveXML()."";
  • The next thing we need to do is fetch the elements from the table.

    Example: If a table has two elements then it should create two XML elements. For that, we will simply use a while loop inside which there will be mysql_fetch_array function to fetch all the data from the table.

    Since the database is connected to a local server it won’t run in your ide but once you have made the database it will work fine. To create the database follow the below mentioned procedure

    • Create a database fitness in mysql using local server.
    • Then create a table user.
    • Then add the columns-uid, uname, email, password, description, role, pic.





    $con=mysqli_connect("localhost", "root", "", "fitness");
        echo "DB not Connected...";
    $result=mysqli_query($con, "Select * from users");
    $xml = new DOMDocument("1.0");
    // It will format the output in xml format otherwise
    // the output will be in a single row
        $uid=$xml->createElement("uid", $row['uid']);
        $uname=$xml->createElement("uname", $row['uname']);
        $email=$xml->createElement("email", $row['email']);
        $password=$xml->createElement("password", $row['password']);
        $description=$xml->createElement("description", $row['description']);
        $role=$xml->createElement("role", $row['role']);
        $pic=$xml->createElement("pic", $row['pic']);
    echo "<xmp>".$xml->saveXML()."</xmp>";
        echo "error";




    My Personal Notes arrow_drop_up

    Check out this Author's contributed articles.

    If you like GeeksforGeeks and would like to contribute, you can also write an article using or mail your article to See your article appearing on the GeeksforGeeks main page and help other Geeks.

    Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.