0

How can I create an XML file from PostgreSQL?

3
  • postgresql is DBMS , please be clear Commented Feb 11, 2011 at 8:54
  • ok? but any code you have jigar.. i m just beginer... Commented Feb 11, 2011 at 11:20
  • @JigarJoshi Yes even if DBMS you can create XML file implicitly.................... Commented Aug 26, 2018 at 7:19

4 Answers 4

8

Lets say you need to create following kind of XML

<Agents>
<agent id="32"/>
<agent id="33"/>
<agent id="34"/>
</Agents>

Then just run following query;

SELECT
    XMLFOREST(tb1."xml-val" AS "Agents")
FROM
    (
        SELECT
            XMLAGG(XMLELEMENT(NAME agent ,XMLATTRIBUTES(t.id AS id))) AS 
            "xml-val"
        FROM
            test t
    ) AS tb1;

:)

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

1 Comment

XMLFOREST is not efficient as it's invalid XML documents. It's just content :(
3

By using the XML functions:

http://www.postgresql.org/docs/current/static/functions-xml.html#AEN15086

4 Comments

hey jigar.. but i want to create xmlfile from postgreas... some exact codind you have?
I'm not "jigar". And what is your question? The functions that are listed in the manual can be used to generate XML "from" Postgres. And the manual contains examples on how to do it. There are pletty of examples in the internet. Just search for "postgres xml generation"
i want to create xml file from database.. i use posgresql.. "select * tablename for xml auto;" is that rite query?? if not than tell me the rite...
You have to apply the functions that are documented in the manual. A select * from table does obviously not return XML
1

Here is a stored procedure (called function in PostgresSQL) that returns XML from a simple query.

CREATE OR REPLACE FUNCTION getXml()
  RETURNS xml 
  AS
$BODY$
DECLARE myXml xml;
BEGIN 
    SELECT * INTO myXml FROM query_to_xml_and_xmlschema('SELECT id FROM someTable', true, true, 'myProject.mySchema');
    RETURN myXml;
END;
$BODY$
  LANGUAGE plpgsql VOLATILE
  COST 100;
ALTER FUNCTION getXml() OWNER TO postgres;

Call the function by the select statement:

SELECT getXml();

The function will return a schema in XSD schema notation, and your data in XML rendered as an 'XML forest'.

Comments

1

This is my precise example to the question.

CREATE TABLE xml_mobile_data AS SELECT
xml $$
    <rows>
        <row id="1">
            <model>Samsung Galaxy Note 9</model>
            <price>$3,000.00</price>
        </row>
        <row id="2">
            <model>iPhone X</model>
            <price>$1,000.00</price>
        </row>
        <row id="3">
            <model>Samsung Galaxy S9+</model>
            <price>$999.00</price>
        </row>
        <row id="4">
            <model>Huawei P20 Pro</model>
            <price>$2,000.00</price>
        </row>
        <row id="5">
            <model>Google Pixel XL 3</model>
            <price>$899.00</price>
        </row>
    </rows>
$$ AS mobile_data;

We can retrieve this in row format.

SELECT xmltable.*
FROM xml_mobile_data,
    XMLTABLE('/rows/row' 
             PASSING mobile_data 
             COLUMNS
                ordinality FOR ORDINALITY,
                model_id INT PATH '@id',
                model TEXT PATH 'model',
                "price" MONEY
    );  

Another way is the usage of WITH QUERY CTE(COMMON TABLE EXPRESSIONS)

WITH xmldata(data) AS (VALUES ('
<rows>
    <row id="1">
        <model>Samsung Galaxy Note 9</model>
        <price>$3,000.00</price>
    </row>
    <row id="2">
        <model>iPhone X</model>
        <price>$1,000.00</price>
    </row>
    <row id="3">
        <model>Samsung Galaxy S9+</model>
        <price>$999.00</price>
    </row>
    <row id="4">
        <model>Huawei P20 Pro</model>
        <price>$2,000.00</price>
    </row>
    <row id="5">
        <model>Google Pixel XL 3</model>
        <price>$899.00</price>
    </row>
</rows>
'::XML))
SELECT xmltable.*
FROM xmldata, 
        XMLTABLE('/rows/row' 
             PASSING data 
             COLUMNS
                ordinality FOR ORDINALITY,
                model_id INT PATH '@id',
                model TEXT PATH 'model',
                "price" MONEY
    );  

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.