Convert database to xml using php [2 easy ways with code]

  1. Previous
  2. Next
  3. Recent

    1. Signs 'He' May be Cheating on You
    2. 7 Vows of Hindu Marriage - Know What 'Saat Vachans' Mean
    3. RuPay vs Visa vs Mastercard vs American Express- A Comparison
    4. What is RuPay? Everything You Need to Know About India's Domestic Payment System!
    5. Top 10 Online Portals to Find a High Paying Job in India
    6. Top Indians who Revolutionized the IT Sector in India
    7. 8 Initiatives by Narendra Modi that Could Change the Future of the Nation
    8. 8 Work-from-home Jobs for Housewives, Students, and Part timers
    9. Complete Company Registration Process in India - Explained
    10. Change Name in India in 5 Steps - Filing Affidavit, Newspaper Ad & Gazette Notification

Every website saves its data to a centralized database. Database records are easy to manage, but slightly difficult to transport.

XML files are better in terms of portability. They can be carried anywhere just like normal text or doc file. They can be read on any web browser. Also, alteration in XML file can be done without using any third party software.

So, its a nice idea to import database records in XML format for easy transportation.

Lets Start,

For the example, we have a MySQL database called ‘test’ with table ‘quiz_techno’ that contains a few questions with unique ids and levels. We’ll retrieve them, convert to XML, and save in a dynamically generated ‘myxml1.xml’ file.

Make connection to database


<?php

 $con=mysql_connect("localhost","root","") or die("Database connection failed");
 if($con)
 {
 mysql_selectdb("test",$con);
 }

?>

Now conversion can be done using 2 methods: the first one is a bit lengthy while the second one is shorter and simpler to understand.

Method-1: Convert using DOMDocument php Class


<?php

$xmldoc=new DOMDocument("1.0", "UTF-8");
$xmlroot=$xmldoc->createElement("questions");
$xmlroot=$xmldoc->appendChild($xmlroot);

$query=mysql_query("SELECT qid, level, question FROM quiz_techno");
while($geteach=mysql_fetch_array($query))
{
$qid=$geteach[0];
$level=$geteach[1];
$question=$geteach[2];

$xmlqa=$xmldoc->createElement("qa");
$xmlqa=$xmlroot->appendChild($xmlqa);

$xmlqa->appendChild($xmldoc->createElement("qid", $qid));
$xmlqa->appendChild($xmldoc->createElement("level", $level));
$xmlqa->appendChild($xmldoc->createElement("question", $question));
}

$xmlfile=fopen("myxml1.xml", "w");
$status=fwrite($xmlfile, $xmldoc->saveXML());
if($status)
{
echo "DATABASE ENTRIES CONVERTED TO XML!";
}
else
{
echo "DATABASE TO XML CONVERSION FAILED!";
}
fclose($xmlfile);

?>

Here, we’re using DOMDocument class of php. Using createElement() method, we create custom xml elements  just like normal HTML elements. Next we place them inside one another as per our desired XML format using appendChild() method.

Finally, we create ‘myxml1.xml’ and write XML data to it.

Method-2: Conversion using SimpleXMLElement Class


<?php

$xmldoc=new SimpleXMLElement("<?xml version='1.0' encoding='UTF-8' ?><questions />");

$query=mysql_query("SELECT qid, level, question FROM quiz_techno");
while($geteach=mysql_fetch_array($query))
{
$qid=$geteach[0];
$level=$geteach[1];
$question=$geteach[2];

$qa=$xmldoc->addChild("qa");
$qa->addChild("qid", $qid);
$qa->addChild("level", $level);
$qa->addChild("question", $question);
}

$xmlfile=fopen("myxml1.xml", "w");
$status=fwrite($xmlfile, $xmldoc->asXML());
if($status)
{
echo "DATABASE ENTRIES CONVERTED TO XML!";
}
else
{
echo "DATABASE TO XML CONVERSION FAILED!";
}
fclose($xmlfile);

?>

Here, alternatively we use SimpleXMLElement php Class (a new addition in PHP5) to convert records of database to XML. This class provides a method addChild() that dynamically create XML elements and place them inside root and child nodes.

The generated XML file

And finally, this is what we get. The XML file containing database records,

blog comments powered by Disqus