{"id":1202,"date":"2016-04-18T20:54:13","date_gmt":"2016-04-18T20:54:13","guid":{"rendered":"http:\/\/62.131.51.129\/?p=1202"},"modified":"2016-04-18T20:54:13","modified_gmt":"2016-04-18T20:54:13","slug":"reading-xml-in-oracle-1","status":"publish","type":"post","link":"http:\/\/archief.van-maanen.com\/?p=1202","title":{"rendered":"Reading XML in Oracle -1"},"content":{"rendered":"<p>As a first example on how to read an XML file, I show how an XML file can be read with a PLSQL programme. The logic is quite simple. We know that XML can be declared as an XMLtype. Once that is known, we declare the full XML file as an object that is read on a record by record base. The values can then be extracted with the ExtractValue procedure. Once the values are extracted, they can be inserted into a table.<\/p>\n<p>We use the XMLTYPE here. This type allows to store XML files in a field. With special methods, one may read, update or delete the content of the XML. One may notice in the procedure below the ExtractValue method that allows to extract a certain value from the XML file.<\/p>\n<pre>create or replace PROCEDURE lees_XML AS<br \/>x XMLType := XMLType(<br \/>'&lt;?xml version=\"1.0\" ?&gt;<br \/>&lt;person&gt;<br \/>&lt;row&gt;<br \/>&lt;name&gt;Tom&lt;\/name&gt;<br \/>&lt;Address&gt;<br \/>&lt;State&gt;California&lt;\/State&gt;<br \/>&lt;City&gt;Los angeles&lt;\/City&gt;<br \/>&lt;\/Address&gt;<br \/>&lt;\/row&gt;<br \/>&lt;row&gt;<br \/>&lt;name&gt;Jim&lt;\/name&gt;<br \/>&lt;Address&gt;<br \/>&lt;State&gt;California&lt;\/State&gt;<br \/>&lt;City&gt;Los angeles&lt;\/City&gt;<br \/>&lt;\/Address&gt;<br \/>&lt;\/row&gt;<br \/>&lt;\/person&gt;');<br \/>BEGIN<br \/>FOR r IN (<br \/>SELECT ExtractValue(Value(p),'\/row\/name\/text()') as name<br \/>,ExtractValue(Value(p),'\/row\/Address\/State\/text()') as state<br \/>,ExtractValue(Value(p),'\/row\/Address\/City\/text()') as city<br \/>FROM TABLE(XMLSequence(Extract(x,'\/person\/row'))) p<br \/>) LOOP<br \/>insert into STORE_XML values(r.name, r.state, r.city);<br \/>END LOOP;<br \/>END;<\/pre>\n<p>Another display is given when we look at a table that has two XML files:<\/p>\n<p><a href=\"http:\/\/62.131.51.129\/wp-content\/uploads\/2016\/04\/Untitled-2.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1211\" src=\"http:\/\/62.131.51.129\/wp-content\/uploads\/2016\/04\/Untitled-2.png\" alt=\"Untitled\" width=\"240\" height=\"64\" \/><\/a><\/p>\n<p>One may want to extract from the second XML file all values that are stored in. That can be done with<\/p>\n<pre>SELECT xt.*\nFROM   xml_tab x,\n       XMLTABLE('\/person\/row'\n         PASSING x.xml_data\n         COLUMNS \n           \"State\"    VARCHAR2(12)  PATH 'Address\/State',\n           \"City\"    VARCHAR2(15)  PATH 'Address\/City',\n           \"Name\"    VARCHAR2(10)  PATH 'name'\n           ) xt where ID=2;\n<\/pre>\n<p>The xml looks like. The \/person\/row is related to the &lt;person&gt; and &lt;row&gt; tag. Within that branch, one has the &lt;name&gt; tag with the &lt;Adress&gt;\/&lt;State&gt; tag. Such tags are used in the query. Beware that the tags are case sensitive.\u00a0<\/p>\n<p>\u00a0<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-2979\" src=\"http:\/\/van-maanen.com\/wp-content\/uploads\/2016\/04\/Untitled-3-1-1.png\" alt=\"\" width=\"394\" height=\"514\" \/><\/p>\n<p>A final word on the clause &#8216;\/person\/row&#8217;. This is an example of a so-called XQuery. This &#8216;\/person\/row&#8217; returns a row. It is possible to make a selection like &#8216;\/person\/row[name=&#8221;Tom&#8221;]&#8217;. This only returns the rows where name equals &#8220;Tom&#8221;.<\/p>\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>As a first example on how to read an XML file, I show how an XML file can be read with a PLSQL programme. The logic is quite simple. We know that XML can be declared as an XMLtype. Once that is known, we declare the full XML file as an object that is read [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":1205,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"class_list":["post-1202","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-nice-to-know"],"_links":{"self":[{"href":"http:\/\/archief.van-maanen.com\/index.php?rest_route=\/wp\/v2\/posts\/1202","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/archief.van-maanen.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/archief.van-maanen.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/archief.van-maanen.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/archief.van-maanen.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1202"}],"version-history":[{"count":0,"href":"http:\/\/archief.van-maanen.com\/index.php?rest_route=\/wp\/v2\/posts\/1202\/revisions"}],"wp:attachment":[{"href":"http:\/\/archief.van-maanen.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1202"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/archief.van-maanen.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1202"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/archief.van-maanen.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1202"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}