{"id":11296,"date":"2016-02-19T09:59:37","date_gmt":"2016-02-19T14:59:37","guid":{"rendered":"http:\/\/jianmingli.com\/wp\/?p=11296"},"modified":"2016-02-19T09:59:37","modified_gmt":"2016-02-19T14:59:37","slug":"generate-xml-from-oracle-database","status":"publish","type":"post","link":"https:\/\/jianmingli.com\/wp\/?p=11296","title":{"rendered":"Generate XML from Oracle Database"},"content":{"rendered":"<div class='toc wptoc'>\n<h2>Contents<\/h2>\n<ol class='toc-odd level-1'>\n\t<li>\n\t\t<a href=\"#Overview\">Overview<\/a>\n\t<\/li>\n\t<li>\n\t\t<a href=\"#Generate_XML_using_SQL_Functions\">Generate XML using SQL Functions<\/a>\n\t<\/li>\n\t<li>\n\t\t<a href=\"#Generate_XML_using_DBMS_XMLGEN\">Generate XML using DBMS_XMLGEN<\/a>\n\t\t<ol class='toc-even level-2'>\n\t\t\t<li>\n\t\t\t\t<a href=\"#dbms-xmlgen_Functions_and_Procedures\">dbms-xmlgen Functions and Procedures<\/a>\n\t\t\t<\/li>\n\t\t\t<li>\n\t\t\t\t<a href=\"#Examples\">Examples<\/a>\n\t\t\t\t<ol class='toc-odd level-3'>\n\t\t\t\t\t<li>\n\t\t\t\t\t\t<a href=\"#Example_One\">Example One<\/a>\n\t\t\t\t\t<\/li>\n\t\t\t\t<\/ol>\n<\/ol>\n\t\t\t<li>\n\t\t\t\t<a href=\"#References\">References<\/a>\n\t\t\t<\/li>\n<\/ol>\n<\/ol>\n<\/div>\n<div class='wptoc-end'>&nbsp;<\/div>\n<span id=\"Overview\"><h2>Overview<\/h2><\/span>\n<span id=\"Generate_XML_using_SQL_Functions\"><h2>Generate XML using SQL Functions<\/h2><\/span>\n<p>* XMLELEMENT and XMLATTRIBUTES<br \/>\n* XMLFOREST<br \/>\n* XMLCONCAT<br \/>\n* XMLAGG<br \/>\n* XMLPI<br \/>\n* XMLCOMMENT<br \/>\n* XMLSERIALIZE<br \/>\n* XMLPARSE<\/p>\n<span id=\"Generate_XML_using_DBMS_XMLGEN\"><h2>Generate XML using DBMS_XMLGEN<\/h2><\/span>\n<p>* DBMS_XMLGEN creates XML documents (as a CLOB or XMLType) from SQL query results<br \/>\n* It provides a <em>fetch<\/em> interface<br \/>\n&#8211; max rows to retrieve<br \/>\n&#8211; max rows to skip<br \/>\n* It provides options for changing tag names for ROW, ROWSET, etc.<\/p>\n<span id=\"dbms-xmlgen_Functions_and_Procedures\"><h3>dbms-xmlgen Functions and Procedures<\/h3><\/span>\n<p>* Context handle<br \/>\n&#8211; used by all functions<\/p>\n<pre lang=\"sql\">\r\nSUBTYPE ctxHandle IS NUMBER\r\n<\/pre>\n<p>* Obtain a context handle<\/p>\n<pre lang=\"sql\">\r\n-- Context handle from query string\r\nnewContext(\r\n  queryString IN VARCHAR2)\r\n\r\n-- Context handle from cursor variable\r\nnewContext(\r\n  queryString IN SYS_REFCURSOR)\r\n  RETURN ctxHandle;\r\n\r\n-- ?\r\nnewContextFromHierarchy(\r\n  queryString IN VARCHAR2)\r\n  RETURN ctxHandle;\r\n<\/pre>\n<pre lang=\"sql\">\r\n  -- Create context handle\r\n  qryCtx := dbms_xmlgen.newContext(\r\n    'select * from hr.employees');\r\n<\/pre>\n<p>* Sets the name of the element separating all the rows. The default name is <em>ROW<\/em>.<\/p>\n<pre lang=\"sql\">\r\nsetRowTag(ctx IN ctxHandle,\r\n          rowTag IN VARCHAR2);\r\n\r\ndbms_xmlgen.setRowTag(qryCtx, 'EMPLOYEE');\r\n<\/pre>\n<p>* Sets the name of the document root element. The default name is <em>ROWSET<\/em><\/p>\n<pre lang=\"sql\">\r\nsetRowSetTag(ctx IN ctxHandle, \r\n             rowSetTag IN VARCHAR2);\r\n\r\n  -- Set the rowset tab\r\n  dbms_xmlgen.setRowSetTag(qryCtx, 'EMPLOYEES');\r\n<\/pre>\n<p>* Retrieves XML<\/p>\n<pre lang=\"sql\">\r\n-- Get xml doc from input context handle and append to input clob:\r\ngetXML(ctx IN ctxHandle, \r\n       clobval IN OUT NCOPY clob, \r\n       dtdOrSchema IN number:= NONE);\r\n\r\n-- Get xml doc from input context handle and return as clob:\r\ngetXML(ctx IN ctxHandle, \r\n       dtdOrSchema IN number:= NONE) \r\n  RETURN clob;\r\n\r\n-- Get xml doc from input context handle and return as XMLType:\r\ngetXMLType(\r\n  ctx IN ctxHandle, \r\n  dtdOrSchema IN number:= NONE)\r\n  RETURN XMLType;\r\n\r\n-- Get xml from input query string and return as clob;\r\ngetXML(\r\n  sqlQuery IN VARCHAR2, \r\n  dtdOrSchema IN NUMBER := NONE)\r\n  RETURN CLOB;\r\n\r\n-- Get xml from input query string and return as XMLType;\r\ngetXMLType(\r\n  sqlQuery IN VARCHAR2, \r\n  dtdOrSchema IN NUMBER := NONE)   \r\n  RETURN XMLType;\r\n<\/pre>\n<p>* Get number of rows processed<\/p>\n<pre lang=\"sql\">\r\ngetNumRowsProcessed(ctx IN ctxHandle) \r\n  RETURN number;\r\n\r\n-- e.g.\r\nexit when dbms_xmlgen.getnumrowsprocessed(qryCtx) = 0;\r\n<\/pre>\n<p>* Sets the maximum number of rows to fetch from the SQL query result for every invocation of the getXML call.<br \/>\n&#8211; It is an error to call this function on a context handle created by function <em>newContextFromHierarchy<\/em>.<\/p>\n<pre lang=\"sql\">\r\nsetMaxRows(ctx IN ctxHandle, \r\n           maxRows IN NUMBER);\r\n\r\n-- e.g.\r\ndbms_xmlgen.setmaxrows(qryCtx, 2);\r\n<\/pre>\n<p>* Skips a given number of rows before generating the XML output for every call to getXML.<br \/>\n&#8211; It is an error to call this function on a context handle created by function <em>newContextFromHierarchy<\/em>.<\/p>\n<pre lang=\"sql\">\r\nsetSkipRows(ctx IN ctxHandle, \r\n            skipRows IN NUMBER);\r\n<\/pre>\n<p>* Determines whether or not special characters in the XML data must be converted into their escaped XML equivalent.<br \/>\n&#8211; defaults to yes<\/p>\n<pre lang=\"sql\">\r\nsetConvertSpecialChars(\r\n  ctx IN ctxHandle, \r\n  conv IN BOOLEAN);\r\n<\/pre>\n<p>* Sets the name of the collection elements.<br \/>\n&#8211; The default name for collection elements it the type name itself.<br \/>\n&#8211; You can override that to use the name of the column with the <em>_ITEM<\/em> tag appended to it using this function.<\/p>\n<pre lang=\"sql\">\r\nuseItemTagsForColl(ctx IN ctxHandle);\r\n<\/pre>\n<p>* Restarts the query and generate the XML from the first row again.<\/p>\n<pre lang=\"sql\">\r\nrestartQuery(ctx IN ctxHandle);\r\n<\/pre>\n<p>* Close context handle<\/p>\n<pre lang=\"sql\">\r\ncloseContext(ctx IN ctxHandle);\r\n<\/pre>\n<p>* Encodes or decodes the XML data string argument.<br \/>\n&#8211; Encoding refers to replacing entity references such as < to their escaped equivalent, such as &lt;.\n- Decoding refers to the reverse conversion.\n\n\n<pre lang=\"sql\">\r\nconvert(\r\n  xmlData IN varchar2, \r\n  flag IN NUMBER := ENTITY_ENCODE)    \r\n  RETURN VARCHAR2;\r\n\r\nconvert(\r\n  xmlData IN CLOB, \r\n  flag IN NUMBER := ENTITY_ENCODE) \r\n  RETURN CLOB;\r\n<\/pre>\n<p>* NULL handling<br \/>\nThe setNullHandling flag values are:<br \/>\n    DROP_NULLS CONSTANT NUMBER := 0;<br \/>\n    This is the default setting and leaves out the tag for NULL elements.<\/p>\n<p>    NULL_ATTR CONSTANT NUMBER := 1;<br \/>\n    This sets xsi:nil = &#8220;true&#8221;.<\/p>\n<p>    EMPTY_TAG CONSTANT NUMBER := 2;<br \/>\n    This sets, for example, <foo\/>.<\/p>\n<pre lang=\"sql\">\r\nsetNullHandling(ctx IN ctxHandle, \r\n                flag IN NUMBER);\r\n\r\n-- shortcut for setNullHandling(ctx, NULL_ATTR)\r\nuseNullAttributeIndicator(\r\n  ctx IN ctxHandle, \r\n  attrind IN BOOLEAN := TRUE);\r\n<\/pre>\n<p>* Sets bind value for the bind variable appearing in the query string associated with the context handle.<br \/>\n&#8211; The query string with bind variables cannot be executed until all of the bind variables are set values using setBindValue.<\/p>\n<pre lang=\"sql\">\r\nsetBindValue(\r\n  ctx IN ctxHandle, \r\n  bindVariableName IN VARCHAR2, \r\n  bindValue IN VARCHAR2);\r\n<\/pre>\n<p>* Clears all the bind values for all the bind variables appearing in the query string associated with the context handle. Afterwards, all of the bind variables must rebind new values using setBindValue.<\/p>\n<pre lang=\"sql\">\r\nclearBindValue(ctx IN ctxHandle);\r\n<\/pre>\n<span id=\"Examples\"><h3>Examples<\/h3><\/span>\n<span id=\"Example_One\"><h4>Example One<\/h4><\/span>\n<p>* Create a temp table to hold XML result:<\/p>\n<pre lang=\"sql\">\r\ncreate table temp_clob_tab (result clob);\r\n<\/pre>\n<p>* Use dbms_xmlgen:<\/p>\n<pre lang=\"sql\">\r\ndeclare\r\n  -- Define a context handle\r\n  qryCtx dbms_xmlgen.ctxHandle;\r\n  -- Result XML\r\n  xml clob;\r\nbegin\r\n  -- Create context handle\r\n  qryCtx := dbms_xmlgen.newContext(\r\n    'select * from hr.employees');\r\n  \r\n  -- Set the row tab\r\n  dbms_xmlgen.setRowTag(qryCtx, 'EMPLOYEE');\r\n  \r\n  dbms_xmlgen.setmaxrows(qryCtx, 2);\r\n  \r\n  loop\r\n    -- Get result xml\r\n    xml := dbms_xmlgen.getXML(qryCtx);\r\n    \r\n    exit when dbms_xmlgen.getnumrowsprocessed(qryCtx) = 0;\r\n  \r\n    -- Insert result xml into temp table\r\n    insert into temp_clob_tab values(xml);\r\n  end loop;\r\n  \r\n  -- Close context\r\n  dbms_xmlgen.closeContext(qryCtx);\r\nend;\r\n\/\r\n\r\n<\/pre>\n<span id=\"References\"><h2>References<\/h2><\/span>\n<p>* <a href=\"http:\/\/docs.oracle.com\/cd\/E11882_01\/appdev.112\/e23094\/xdb13gen.htm#ADXDB1600\">Generating XML Data from the Database<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Overview Generate XML using SQL Functions * XMLELEMENT and XMLATTRIBUTES * XMLFOREST * XMLCONCAT * XMLAGG * XMLPI * XMLCOMMENT * XMLSERIALIZE * XMLPARSE Generate XML using DBMS_XMLGEN * DBMS_XMLGEN creates XML documents (as a CLOB or XMLType) from SQL &hellip; <a href=\"https:\/\/jianmingli.com\/wp\/?p=11296\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_exactmetrics_skip_tracking":false,"_exactmetrics_sitenote_active":false,"_exactmetrics_sitenote_note":"","_exactmetrics_sitenote_category":0,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[35],"tags":[567,674],"class_list":["post-11296","post","type-post","status-publish","format-standard","hentry","category-oracle","tag-oracle","tag-xml"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p8cRUO-2Wc","_links":{"self":[{"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/posts\/11296","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=11296"}],"version-history":[{"count":2,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/posts\/11296\/revisions"}],"predecessor-version":[{"id":11313,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/posts\/11296\/revisions\/11313"}],"wp:attachment":[{"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=11296"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=11296"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=11296"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}