Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Postgres Data to XML

Tags:

xml

postgresql

Does anyone know any good methods for converting database entries into XML?

I.e. if i have a table named "Users" with fields "first_name", "age", "last_name", I'd like to convert the table to:

<Users>
  <first_name>Papa</first_name>
  <age>50</age>
  <last_name>John</last_name>
</Users>
like image 823
Tian Avatar asked Aug 24 '10 18:08

Tian


People also ask

Does PostgreSQL support XML?

Again, according to the SQL standard, this is the only way to convert between type xml and character types, but PostgreSQL also allows you to simply cast the value. SET xmloption TO { DOCUMENT | CONTENT }; The default is CONTENT , so all forms of XML data are allowed.

Is PostgreSQL OS dependent?

PostgreSQL can be expected to work on these operating systems: Linux (all recent distributions), Windows (XP and later), FreeBSD, OpenBSD, NetBSD, macOS, AIX, HP/UX, and Solaris.


2 Answers

In PostgreSQL you could it like this:

SELECT table_to_xml('users', true, false, '');

Or

SELECT query_to_xml('SELECT * FROM users', true, false, '');

There are other options as well, just check the manual.

like image 142
Frank Heikens Avatar answered Oct 21 '22 06:10

Frank Heikens


This is a question independent of the DB it can be done with any DB supported by ActiveRecord.

User.find(some_id).to_xml(:except => [:id,:created_at,:updated_at])

The :except => [:id,:created_at,:updated_at] removes the Rails default columns from the XML output.

There is an interesting blog post about this matter: http://ryandaigle.com/articles/2007/4/13/what-s-new-in-edge-rails-a-more-flexible-to_xml

like image 27
jigfox Avatar answered Oct 21 '22 07:10

jigfox