{"id":56,"date":"2010-01-19T17:42:00","date_gmt":"2010-01-19T16:42:00","guid":{"rendered":"https:\/\/ilmarkerm.eu\/blog\/2010\/01\/the-easy-way-of-working-with-xml-in-oracle-database-part-1\/"},"modified":"2010-01-19T17:42:00","modified_gmt":"2010-01-19T16:42:00","slug":"the-easy-way-of-working-with-xml-in-oracle-database-part-1","status":"publish","type":"post","link":"https:\/\/ilmarkerm.eu\/blog\/2010\/01\/the-easy-way-of-working-with-xml-in-oracle-database-part-1\/","title":{"rendered":"The easy way of working with XML in Oracle database (part 1)"},"content":{"rendered":"<p>I have tried to read Oracle XMLDB Developer Guide and my first reaction was that parsing XML is very complicated in Oracle&#8230; And lately I got a task to rewrite some PL\/SQL code to use a different SOAP service and the existing code had a few hundred rows just to parse XML (with XML DOM API)! Actually, starting from 10.2, this job is much easier&#8230;<\/p>\n<p>Here I will go over some methods for working with XML, that are usable directly from SQL. In this first part, reading and extracting data from XML.<\/p>\n<h2>XML for the examples<\/h2>\n<pre>\n&lt;?xml version=\"1.0\" encoding=\"utf-8\"?&gt;\n<company>\n  <name>Some Company name<\/name>\n  <employees>\n    <item>\n      <first_name>Ilmar<\/first_name>\n      <last_name>Kerm<\/last_name>\n      <country>Estonia<\/country>\n      <departments>\n        <item active=\"false\">\n          <name>Development<\/name>\n        <\/item>\n        <item active=\"true\">\n          <name>DBA<\/name>\n        <\/item>\n      <\/departments>\n    <\/item>\n    <item>\n      <first_name>Ilmar2<\/first_name>\n      <last_name>Kerm2<\/last_name>\n      <country>Estonia2<\/country>\n      <departments>\n        <item active=\"true\">\n          <name>Development<\/name>\n        <\/item>\n        <item active=\"false\">\n          <name>DBA<\/name>\n        <\/item>\n      <\/departments>\n    <\/item>\n  <\/employees>\n<\/company>\n<\/pre>\n<p>Load the XML data to a table, to special XMLTYPE data type.<\/p>\n<pre>\ncreate table xml (\n  x xmltype\n);\n\ninsert into xml values (\nxmltype.createxml('&lt;?xml version=\"1.0\" encoding=\"utf-8\"?&gt;\n<company>\n  <name>Some Company name<\/name>\n  <employees>\n...\n  <\/employees>\n<\/company>'));\n\nCOMMIT;\n<\/pre>\n<h2>Extracting a single value<\/h2>\n<p><b>extractValue<\/b> can be used to extract a single value from XML using <a href=\"http:\/\/www.w3schools.com\/Xpath\/\">XPath expression<\/a>.<\/p>\n<pre>\nSQL> SELECT extractValue(x, '\/company\/name') FROM xml;\n\nEXTRACTVALUE(X,'\/COMPANY\/NAME')\n-------------------------------\nSome Company name\n<\/pre>\n<p>extractValue only works with single values, otherwise exception will be raised.<\/p>\n<pre>\nSQL> SELECT extractValue(x, '\/company\/employees\/item') FROM xml;\nSELECT extractValue(x, '\/company\/employees\/item') FROM xml\n*\nERROR at line 1:\nORA-19025: EXTRACTVALUE returns value of only one node\n<\/pre>\n<h2>Query XML as relational object<\/h2>\n<p>Oracle Database, since 10g, has a very easy way to map XML to a relational object and query it with SQL, so no PL\/SQL code is needed for parsing XML &#8211; the XMLTABLE function.<\/p>\n<pre>\nSQL> SELECT emp.*\n  FROM xml,\n       XMLTABLE (\n         '\/company\/employees\/item'\n         PASSING x\n         COLUMNS id NUMBER PATH '@id',\n                 first_name VARCHAR2 (10 CHAR) PATH 'first_name',\n                 last_name VARCHAR2 (10 CHAR) PATH 'last_name',\n                 country VARCHAR2 (10 CHAR) PATH 'country',\n                 active_department VARCHAR2 (20 CHAR) PATH 'departments\/item[@active=\"true\"]\/name'\n       ) emp;\n\n        ID FIRST_NAME LAST_NAME  COUNTRY    ACTIVE_DEPARTMENT\n---------- ---------- ---------- ---------- --------------------\n         1 Ilmar      Kerm       Estonia    DBA\n         2 Ilmar2     Kerm2      Estonia2   Development\n<\/pre>\n<p>XmlTable takes the following arguments:<\/p>\n<table border=\"1\">\n<tr>\n<td>&#8216;\/company\/employees\/item&#8217;<\/td>\n<td>XPath expression of the &#8220;row&#8221;<\/td>\n<\/tr>\n<tr>\n<td>PASSING x<\/td>\n<td>Source of XML data &#8211; XMLTYPE data type<\/td>\n<\/tr>\n<tr>\n<td>COLUMNS<br \/>id NUMBER PATH &#8216;@id&#8217;,<br \/>\nfirst_name VARCHAR2 (10 CHAR) PATH &#8216;first_name&#8217;,<br \/>\nlast_name VARCHAR2 (10 CHAR) PATH &#8216;last_name&#8217;,<br \/>\ncountry VARCHAR2 (10 CHAR) PATH &#8216;country&#8217;,<br \/>\nactive_department VARCHAR2 (20 CHAR) PATH &#8216;departments\/item[@active=&#8221;true&#8221;]\/name&#8217;\n<\/td>\n<td>Column definitions with the corresponding Oracle data type and XPath expression<\/td>\n<\/tr>\n<\/table>\n<p>After XML is readable as a relational table, all the power of Oracle SQL can be used for querying.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I have tried to read Oracle XMLDB Developer Guide and my first reaction was that parsing XML is very complicated in Oracle&#8230; And lately I got a task to rewrite some PL\/SQL code to use a different SOAP service and the existing code had a few hundred rows just to parse XML (with XML DOM [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-56","post","type-post","status-publish","format-standard","hentry","category-blog-entry"],"_links":{"self":[{"href":"https:\/\/ilmarkerm.eu\/blog\/wp-json\/wp\/v2\/posts\/56","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ilmarkerm.eu\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ilmarkerm.eu\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ilmarkerm.eu\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ilmarkerm.eu\/blog\/wp-json\/wp\/v2\/comments?post=56"}],"version-history":[{"count":0,"href":"https:\/\/ilmarkerm.eu\/blog\/wp-json\/wp\/v2\/posts\/56\/revisions"}],"wp:attachment":[{"href":"https:\/\/ilmarkerm.eu\/blog\/wp-json\/wp\/v2\/media?parent=56"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ilmarkerm.eu\/blog\/wp-json\/wp\/v2\/categories?post=56"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ilmarkerm.eu\/blog\/wp-json\/wp\/v2\/tags?post=56"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}