{"id":1106,"date":"2019-08-19T15:09:46","date_gmt":"2019-08-19T13:09:46","guid":{"rendered":"https:\/\/mjr.gmbh\/post\/creating-xml-files-via-sql\/"},"modified":"2023-03-15T09:39:33","modified_gmt":"2023-03-15T08:39:33","slug":"creating-xml-files-via-sql","status":"publish","type":"post","link":"https:\/\/www.mjr.gmbh\/en\/post\/tutorials-en\/creating-xml-files-via-sql\/","title":{"rendered":"Creating XML-Files via SQL"},"content":{"rendered":"<p><a ref=\"magnificPopup\" href=\"https:\/\/mjr.gmbh\/wp-content\/uploads\/2016\/04\/xmlwithsql.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-397 size-full\" src=\"https:\/\/mjr.gmbh\/wp-content\/uploads\/2016\/04\/xmlwithsql.png\" alt=\"xmlwithsql Thumbnail\" width=\"700\" height=\"467\" srcset=\"https:\/\/www.mjr.gmbh\/wp-content\/uploads\/2016\/04\/xmlwithsql.png 700w, https:\/\/www.mjr.gmbh\/wp-content\/uploads\/2016\/04\/xmlwithsql-300x200.png 300w\" sizes=\"auto, (max-width: 700px) 100vw, 700px\" \/><\/a><\/p>\n<p>We will show you how to easily convert content of your database tables into an XML file.<\/p>\n<p>And all it takes is just a SQL statement and a CL command. We start off by preparing a sample table, this one will have three columns and three rows for testing:<\/p>\n<div>\n<pre>CREATE TABLE qtemp.TESTXML (FIELD1 CHAR (10 ) , FIELD2 CHAR (256 ), FIELD3 NUMERIC (10 , 3));\r\nINSERT INTO qtemp.TESTXML VALUES('Record1', 'Description1', 1);\r\nINSERT INTO qtemp.TESTXML VALUES('Record2', 'Description2', 2);\r\nINSERT INTO qtemp.TESTXML VALUES('Record3', 'Description3', 3);<\/pre>\n<p>After executing the commands from above, our sample table has been created.<\/p>\n<table border=\"3\">\n<tbody>\n<tr>\n<td>Record1<\/td>\n<td>Description1<\/td>\n<td>1.000<\/td>\n<\/tr>\n<tr>\n<td>Record2<\/td>\n<td>Description2<\/td>\n<td>2.000<\/td>\n<\/tr>\n<tr>\n<td>Record3<\/td>\n<td>Description3<\/td>\n<td>3.000<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n<p>Additionally, we need a temporary table to store our XML data:<\/p>\n<pre>create table qtemp.myxml as (select xmlserialize( \r\n                                    xmlelement(NAME \"MyXMLRecord\", xmlelement(NAME \"RecNbr\", field3), \r\n                                    xmlelement(NAME \"RecordName\", trim(field1)),\r\n                                    xmlelement(NAME \"RecordDescription\", trim(field2))) \r\n                              as char(1024)) as \"XMLResult\" from qtemp.testxml) with data;\r\n\r\n<\/pre>\n<p>As soon as this is done, our data has been stored within the table and we just have to export them into a text file in the IFS. You can do this by using the following CL command:<\/p>\n<pre>CPYTOIMPF FROMFILE(MYXML) TOSTMF('\/myxml.xml') MBROPT(*REPLACE) FROMCCSID(1252) RCDDLM(*CRLF) STRDLM(*NONE)<\/pre>\n<p>The final result will look like this:<\/p>\n<pre>&lt;MyXMLRecord&gt;&lt;RecNbr&gt;1.000&lt;\/RecNbr&gt;&lt;RecordName&gt;Record1&lt;\/RecordName&gt;&lt;RecordDescription&gt;Description1&lt;\/RecordDescription&gt;&lt;\/MyXMLRecord&gt; \r\n&lt;MyXMLRecord&gt;&lt;RecNbr&gt;2.000&lt;\/RecNbr&gt;&lt;RecordName&gt;Record2&lt;\/RecordName&gt;&lt;RecordDescription&gt;Description2&lt;\/RecordDescription&gt;&lt;\/MyXMLRecord&gt; \r\n&lt;MyXMLRecord&gt;&lt;RecNbr&gt;3.000&lt;\/RecNbr&gt;&lt;RecordName&gt;Record3&lt;\/RecordName&gt;&lt;RecordDescription&gt;Description3&lt;\/RecordDescription&gt;&lt;\/MyXMLRecord&gt;<\/pre>\n<p>Other structures within the XML can easily be created by using the respective keywords.<\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>We will show you how to easily convert content of your database tables into an XML file. And all it takes is just a SQL statement and a CL command. We start off by preparing a sample table, this one will have three columns and three rows for testing: CREATE TABLE qtemp.TESTXML (FIELD1 CHAR (10&#8230;<\/p>\n","protected":false},"author":18,"featured_media":397,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[18],"tags":[],"tutorial_kategorie":[],"class_list":["post-1106","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-tutorials-en"],"acf":[],"views":5843,"_links":{"self":[{"href":"https:\/\/www.mjr.gmbh\/en\/wp-json\/wp\/v2\/posts\/1106","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.mjr.gmbh\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.mjr.gmbh\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.mjr.gmbh\/en\/wp-json\/wp\/v2\/users\/18"}],"replies":[{"embeddable":true,"href":"https:\/\/www.mjr.gmbh\/en\/wp-json\/wp\/v2\/comments?post=1106"}],"version-history":[{"count":0,"href":"https:\/\/www.mjr.gmbh\/en\/wp-json\/wp\/v2\/posts\/1106\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.mjr.gmbh\/en\/wp-json\/wp\/v2\/media\/397"}],"wp:attachment":[{"href":"https:\/\/www.mjr.gmbh\/en\/wp-json\/wp\/v2\/media?parent=1106"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.mjr.gmbh\/en\/wp-json\/wp\/v2\/categories?post=1106"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.mjr.gmbh\/en\/wp-json\/wp\/v2\/tags?post=1106"},{"taxonomy":"tutorial_kategorie","embeddable":true,"href":"https:\/\/www.mjr.gmbh\/en\/wp-json\/wp\/v2\/tutorial_kategorie?post=1106"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}