Create XML through php and mysql

Here I am giving you the very basic sample code for creating the XML file through PHP and MYsql


// We'll be outputting a PDF
header('Content-type: text/xml');

echo "”

$db_name = “testDB”;
$connection = mysql_connect(“”, “username”, “password”) or die(“Could not connect.”);
$table_name = ‘user’;

$query = “select * from ” . $table_name;

$result = mysql_query($query, $connection) or die(“Could not complete database query”);
$num = mysql_num_rows($result);

while ($row = mysql_fetch_assoc($result)) {
echo “”. $row[‘firstname’].””;
echo “”. $row[‘lastname’].””;
echo “

“. $row[‘address’].”

echo “”. $row[‘age’].””;



Create XML document with Ruby -XML parsing

XML is most popular data transfer layer.

In every language we need XML parsing. In Ruby there is in build XML strong parsing support.

In this post I am going to cover the How to create XML file with simple Ruby code.

TIP: If you are new in Rails than follow step one or go to step two.

1. You need to install Ruby on your computer

2. Open command prompt(for windows) or console(linux)

3. Create file named CreateXML.rb and copy and paste following code

require “rexml/document”
include REXML

string = <<EOF
<element attribute=”attr”>first XML document with ruby </element>
doc = string

print doc

Output will be like this: