snippetsqlMinor
xml parse in postgresSQL
Viewed 0 times
postgressqlxmlparse
Problem
I have a problem with parse simple xml:
This XML file I parsed with use code:
Unfortunately, this parse gives me only first single row in result.
I need create table where are all record in to relevant rows:
Thank you for your tips.
1
IT
1
2
3
4
5
Pražská
platba kartou
This XML file I parsed with use code:
DO $
DECLARE myxml xml;
BEGIN
myxml := XMLPARSE(DOCUMENT convert_from(pg_read_binary_file('MyData.xml'), 'UTF8'));
DROP TABLE IF EXISTS my;
CREATE TABLE my AS
SELECT
(xpath('//ID', x))[1]::text AS ID,
(xpath('data-set/@Name', x))[1]::text AS Name,
(xpath('//ID_CUSTOMER', x))[1]::text AS id_customer,
(xpath('//Adress', x))[1]::text AS Adress,
(xpath('//Desc', x))[1]::text AS tgen
FROM unnest(xpath('//data-set', myxml)) x
;
END$;
select * from myUnfortunately, this parse gives me only first single row in result.
I need create table where are all record in to relevant rows:
Rows1 - ID 1, ID_CUSTOMER 1, Adress Pražská, Desc Platba kartou
Rows2 - ID 1, ID_CUSTOMER 2, Adress Pražská, Desc Platba kartou
Rows3 - ID 1, ID_CUSTOMER 3, Adress Pražský, Desc Platba kartou
Rows4 - ........
Rows5 - ........Thank you for your tips.
Solution
if I correct I understand You - Your problem is single row in result
You can use unnest function for return all values
EDIT Text of query after questions from author:
You can use unnest function for return all values
EDIT Text of query after questions from author:
DO $
DECLARE xml_string xml;
BEGIN
xml_string := XMLPARSE(DOCUMENT convert_from(pg_read_binary_file('MyData.xml'), 'UTF8'));
DROP TABLE IF EXISTS my;
CREATE TABLE my AS
SELECT
unnest(xpath('//ID/text()', xml_string)) as ID,
unnest(xpath('//Name/text()', xml_string)) as Name,
unnest(xpath('//ID_CUSTOMER/text()', xml_string)) as ID_CUSTOMER;
END$
SELECT * FROM my;
1 IT 1
1 IT 2
1 IT 3
1 IT 4
1 IT 5Code Snippets
DO $$
DECLARE xml_string xml;
BEGIN
xml_string := XMLPARSE(DOCUMENT convert_from(pg_read_binary_file('MyData.xml'), 'UTF8'));
DROP TABLE IF EXISTS my;
CREATE TABLE my AS
SELECT
unnest(xpath('//ID/text()', xml_string)) as ID,
unnest(xpath('//Name/text()', xml_string)) as Name,
unnest(xpath('//ID_CUSTOMER/text()', xml_string)) as ID_CUSTOMER;
END$$
SELECT * FROM my;
1 IT 1
1 IT 2
1 IT 3
1 IT 4
1 IT 5Context
StackExchange Database Administrators Q#155121, answer score: 2
Revisions (0)
No revisions yet.