Friday 13 November 2020

SQL: Get the results in xml format

Most of the databases support to retrieve the data in json, xml formats. In this post, I am going to show how to retrieve the data in xml format.

 

Step 1: Connect to mysql server using --xml option by executing below command.

 

mysql -u root -p --xml

bash-3.2$ mysql -u root -p --xml
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 11
Server version: 8.0.21 Homebrew

Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>

Step 2: List all the databases by executing the command ‘show databases’.

mysql> show databases;
<?xml version="1.0"?>

<resultset statement="show databases;" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <row>
	<field name="Database">demo</field>
  </row>

  <row>
	<field name="Database">information_schema</field>
  </row>

  <row>
	<field name="Database">japanese_orders</field>
  </row>

  <row>
	<field name="Database">mysql</field>
  </row>

  <row>
	<field name="Database">performance_schema</field>
  </row>

  <row>
	<field name="Database">sys</field>
  </row>
</resultset>
6 rows in set (0.00 sec)

mysql>

Step 3: Let me connect to demo database by executing the command ‘use demo’.

mysql> use demo;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed

Step 4: Execute the command ‘show tables’ to list all the tables in demo database.

mysql> show tables;
<?xml version="1.0"?>

<resultset statement="show tables;" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <row>
  <field name="Tables_in_demo">student</field>
  </row>
</resultset>
1 row in set (0.00 sec)

Step 5: Print all the records in student table.

mysql> select * from student;
<?xml version="1.0"?>

<resultset statement="select * from student;" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <row>
  <field name="student_id">63</field>
  <field name="first_name">sailu</field>
  <field name="last_name">ptr</field>
  <field name="email">ptr@abcdef.com</field>
  </row>

  <row>
  <field name="student_id">123</field>
  <field name="first_name">krishna</field>
  <field name="last_name">gurram</field>
  <field name="email">krishna@abcdef.com</field>
  </row>

  <row>
  <field name="student_id">523</field>
  <field name="first_name">gopi</field>
  <field name="last_name">battu</field>
  <field name="email">gopi@abcdef.com</field>
  </row>

  <row>
  <field name="student_id">524</field>
  <field name="first_name">chamu</field>
  <field name="last_name">M</field>
  <field name="email">chamu@abcdef.com</field>
  </row>
</resultset>
4 rows in set (0.00 sec)

mysql>






Previous                                                    Next                                                    Home

No comments:

Post a Comment