Generating XML Files Dynamically in PHP and MySQL

In this era of data-driven applications, generating XML files dynamically is a common requirement. XML (eXtensible Markup Language) is widely used for data exchange between applications and platforms due to its flexibility and compatibility.

In this post, we will explore how to generate XML files dynamically using PHP and MySQL. By the end, you’ll have a solid understanding of the process, along with practical examples to guide you.

Before diving into the code, let’s establish a basic understanding of XML. XML is a markup language that uses tags to define elements and their relationships within a document. These tags enclose the data, providing a structured format. XML files consist of a hierarchical structure, making them suitable for representing complex data sets.

Getting Data from Database

To get started, create a new PHP file and establish a connection to your MySQL database using the MySQL extension. Make sure to include the necessary credentials to access your database.

Here, let’s assume we have users data in our database table and we want to create an XML file out of it. So, we need to fetch the data from the MySQL database that we want to include in our XML file.

Use the appropriate SQL query to retrieve the desired data. For example, let’s assume we have a table named “users” with columns like “id,” “name,” and “email.”. Let’s create a SELECT query for all user data with specific

$hostname = "localhost";
$username = "your_username";
$password = "your_password";
$database = "your_database";

$connection = mysqli_connect($hostname, $username, $password, $database);

if (!$connection) {
    die("Connection failed: " . mysqli_connect_error());

$query = "SELECT id, name, email FROM users";
$result = mysqli_query($connection, $query);

Generating the XML File

Now, we have our data from the database. Let’s generate an XML file dynamically. Here, we will use DOMDocument for creating XML files. Start by creating a new DOMDocument object, which will serve as the root element for our XML file. We’ll loop through the retrieved data and create XML elements accordingly.

$dom = new DOMDocument();

$root = $dom->createElement("users");

while ($row = mysqli_fetch_assoc($result)) {
  $user = $dom->createElement("user");

  $id = $dom->createElement("id", $row["id"]);

  $name = $dom->createElement("name", $row["name"]);

  $email = $dom->createElement("email", $row["email"]);


Saving the XML File

Once the XML file structure is ready, we need to save it to a file. Specify the file path and use the save() method of the DOMDocument object to write the XML content to the file.

$file = "users.xml";



echo "XML file generated successfully!";


In this post, we have generated XML files dynamically with the help of PHP and MySQL. However, you can use those exported files as per requirement or use this logic to create a sitemap automatically or dynamically. Feel free to experiment further, adding more complex elements and attributes to enhance the XML representation.