0

I want to convert my entire MySQL Table into XML... Can anyone Help me out with a tutorial or code.

2
  • Do you want to do this once, or routinely through a web interface? Commented Jul 16, 2009 at 20:51
  • use phpMyAdmin to export in various formats including XML Commented Jul 16, 2009 at 20:56

5 Answers 5

3

mysqldump is able to dump data in XML format ; so, with something like this, you might get what you want :

mysqldump --user=USER --password=PASSWORD --host=HOST --xml DATABASE TABLE

For more information, see mysqldump

Sign up to request clarification or add additional context in comments.

2 Comments

What if i need it automatically each and every time
each and every time... that what occurs ? An insertion / update / deletion ? Well... If your table is big, you should probably not use this too often ^^ Can't you get the dump generated, say, once every day (or hour), via a cron job ? If the table is small and you need this xml dump always up to date... maybe a solution not based on mysqldump (ie, based on code) would be better...
3

For a simple highscore list for an iPhone game I wrote a small php thingy...its output can be viewed here

<highscores>
<?php   
// connection information
  $hostName = "xxxx";
  $userName = "xxxx";
  $password = "xxxx";
  $dbName = "xxxx";

  mysql_connect($hostName, $userName, $password) 
                 or die("Unable to connect to host $hostName");

  mysql_select_db($dbName) or die( "Unable to select database $dbName");

  $query = "SELECT * FROM HIGHSCORES ORDER BY Distance, Fuel DESC LIMIT 10";

  $result = mysql_query($query);

  $number = mysql_numrows($result);

  for ($i=0; $i<$number; $i++) {

    print "  <entry>\n";
    printf("    <Num>%d</Num>\n", $i+1);;

    $Nic = mysql_result($result, $i, "Nic");
    print "    <Nic>$Nic</Nic>\n";

    $Distance = mysql_result($result, $i, "Distance");
    printf( "    <Distance>%.9f</Distance>\n", $Distance);

    $Fuel = mysql_result($result, $i, "Fuel");
    printf( "    <Fuel>%.9f</Fuel>\n", $Fuel);

    print "  </entry>\n";

  }

  mysql_close();

?>
</highscores>

4 Comments

But i am not able to construe the $nic or distance... how will actually call my field names with attributes.
You want it to create XML entries automatically without knowing the table structure? This adds another layer of complexity...I know my table and output only what I need, the table is actually bigger and holds more information.
I need a to create XML entries for my field names with values.
"Nic" and "Distance" are fields in the table. $Nic and $Distance are variables that holds an entries values for these fields. They are then printed (outputted) in a XML format...
1

mysql outputs an XML document if you use -X or --xml option:

mysql -X -e "SELECT * FROM mytable" dbname

Comments

0

The mysql documentation has a page on using XML with MySQL. It would be easy to use that method with a system call to get the data already as XML and save yourself some trouble.

Comments

0

I haven't used the feature, but XMLSpy can create a schema from the database structure. It will also convert data or map it to XML based on an existing DTD or schema.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.