In this post I am going to demonstrate the XML type of PostgreSQL.
Explain with sample example.
What is XML Type ?
The XML Data type is used to store XML data. XML is well formed storage structure of data. You can store multiple node into a single XML column. You can store data into hierarchical format.
User preferences table, in which User can select multiple preferences and even User can create custom preferences for them.
Now how you can store this preferences data for those Users.
Simple, create one XML column and store all User preferences id into this. like,
Below is a demonstration of XML with PostgreSQL.
Basically you can store XML data with one child node or with different child node.
Les’s First start with one child node:
Create table with XML type:
CREATE TABLE XMLTEST (Name XML);
Now Insert XML Data.
INSERT INTO XMLTEST
Select data from XMLTEST:
SELECT *FROM XMLTEST
You can find above full XML into one column.
Now time to parse this XML column using unnest() function of PostgreSQL.
SELECT unnest(xpath('.//Sub/text()', Name::xml))::text AS XMLDATA FROM XMLTEST;
You can see this is four rows now, as we have stored different four values into one XML Column and after parse we can see those four values in the form of different row.
Demonstration of different child node:
INSERT INTO XMLTEST VALUES
unnest(xpath('//Name/text()', Name::XML)) AS Name
,unnest(xpath('//City/text()', Name::XML)) AS City
,unnest(xpath('//Gender/text()', Name::XML)) AS Gender
,unnest(xpath('//Age/text()', Name::XML)) AS Age
Result is :
In above code as we have inserted one XML data with different child node.
You can see the result of one row with different column for different child node.
Other Related Articles,
Please share your ideas and opinions about this topic with me, your contribution will add true value to this topic.
If anyone has doubts on this topic then please do let me know by leaving comments or send me an email.
If you like this post, then please share it with others.
Please follow dbrnd.com, I will share my experience towards the success of Database Research and Development Activity.
I put up a post every day, please keep reading and learning.
Discover Yourself, Happy Blogging !
Anvesh M. Patel.