Selecting (XML) data directly from disk

Marco Gralike 5
0 0
Read Time:2 Minute, 54 Second

On the OTN XMLDB Forum, a question was asked if you could directly select from a XML file on disk, or to be more precise ”
XQUERY / XMLTABLE Syntax to read and XML from a Windows local drive?“. I had the idea that this could be easily done, nowadays via XMLDB functionality. So I tried it and came to the following conclusions. Although tested on Oracle 11.1.0.6.0 on Windows, this should also work on Oracle 10.2.x.

SQL> conn test/test
Connected.
 
SQL> -- The test user has the DBA role...
 
SQL> select * from v$version;
 
BANNER
-----------------------------------------------------------------------
Oracle Database 11g Enterprise Edition Release 11.1.0.6.0 - Production
PL/SQL Release 11.1.0.6.0 - Production
CORE    11.1.0.6.0      Production
TNS for 32-bit Windows: Version 11.1.0.6.0 - Production
NLSRTL Version 11.1.0.6.0 - Production
 
5 rows selected.
 
/* The content of the data.xml file.

<?xml version="1.0" encoding="UTF-8"?>
<ROOT>
  <ID>0</ID>
  <INFO>
    <INFO_ID>0</INFO_ID>
    <INFO_CONTENT>Text</INFO_CONTENT>
  </INFO>
</ROOT>

*/
 
SQL> drop directory  xmlstore;
 
Directory dropped.
 
 
SQL> -- the directory is on a Windows system...
 
SQL> create directory xmlstore as 'E:\temp';
 
Directory created.
 
SQL> create table test
  2  (xmldata xmltype);
 
Table created.
 
SQL> INSERT into test
  2  VALUES
  3  (XMLTYPE(bfilename('XMLSTORE','data.xml'),NLS_CHARSET_ID('AL32UTF8')));
 
1 row created.
 
 
SQL> set long 100000000
 
SQL> select * from test;
 
XMLDATA
--------------------------------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<ROOT>
  <ID>0</ID>
  <INFO>
    <INFO_ID>0</INFO_ID>
    <INFO_CONTENT>Text</INFO_CONTENT>
  </INFO>
</ROOT>
 
 
1 row selected.
 
SQL> select XMLTYPE('<ROOT><ID>0</ID><INFO><INFO_ID>0</INFO_ID><INFO_CONTENT>Text</INFO_CONTENT></INFO></ROOT>') as "XDATA"
  2  from dual;
 
XDATA
------------------------------------------------------------------------------------------
<ROOT><ID>0</ID><INFO><INFO_ID>0</INFO_ID><INFO_CONTENT>Text</INFO_CONTENT></INFO></ROOT>
 
1 row selected.
 
 
SQL> select XMLTYPE(bfilename('XMLSTORE','data.xml'),NLS_CHARSET_ID('AL32UTF8')) as "XDATA"
  2  from dual;
ERROR:
ORA-21500: internal error code, arguments: [%s], [%s], [%s], [%s], [%s], [%s], [%s], [%s]
 
 
 
no rows selected
 
 
SQL> select extract((XMLTYPE(bfilename('XMLSTORE','data.xml'),NLS_CHARSET_ID('AL32UTF8'))),'*') as "XDATA"
  2  from dual;
 
XDATA
------------------------------------------------------------------------------------------
<ROOT><ID>0</ID><INFO><INFO_ID>0</INFO_ID><INFO_CONTENT>Text</INFO_CONTENT></INFO></ROOT>
 
1 row selected.
 
SQL> select xdata
  2  from (XMLTABLE('*'
  3                 PASSING (XMLTYPE(bfilename('XMLSTORE','data.xml'),NLS_CHARSET_ID('AL32UTF8')))
  4                 COLUMNS xdata xmltype PATH '/*'
  5                )
  6       )
  7  ;
 
XDATA
------------------------------------------------------------------------------------------
<ROOT><ID>0</ID><INFO><INFO_ID>0</INFO_ID><INFO_CONTENT>Text</INFO_CONTENT></INFO></ROOT>

 

Cheers

 

Marco

About Post Author

Marco Gralike

Marco Gralike, working for AMIS Services BV as a Principal Oracle Database Consultant in the Netherlands, has experience as a DBA since 1994 (Oracle 6). Marco is also eager and skillful in other fields, like Operating System Administration and Application Servers, mainly to find working, performing solutions. Marco has been specializing in Oracle XMLDB, since 2003, focusing on his old love, database administration and performance.He is an Oracle XMLDB enthusiast ever since. He is also a dedicated contributor of the Oracle User Group community, helping people with their steep XMLDB learning curve. To this purpose, Marco also devoted his personal blog site to XMLDB and other Oracle issues. Marco is a member of the OakTable network and an Oracle ACE Director (specialization Oracle XMLDB).
Happy
Happy
0 %
Sad
Sad
0 %
Excited
Excited
0 %
Sleepy
Sleepy
0 %
Angry
Angry
0 %
Surprise
Surprise
0 %

Average Rating

5 Star
0%
4 Star
0%
3 Star
0%
2 Star
0%
1 Star
0%

5 thoughts on “Selecting (XML) data directly from disk

  1. Hello Marco,
    I have some questions about your article:
    Can give any indication about the performance of this read action? Where is the greatest restriction in the performance?
    Are there any restrictions on the file sizes other than the OS?
    And in which circumstances would you preferably use this technique?

    Cheers,
    Karin

  2. On 10.2.0.2.0 it works without the extract. Note by the way how the encoding is changed to “ISO-8859-1”

    [pre]

    PGI@pgio> select XMLTYPE(bfilename(‘XMLSTORE’,’data.xml’),NLS_CHARSET_ID(‘AL32UTF8’)) as “XDATA”
    2 from dual
    3 /

    XDATA
    ——————————————————————————–

    0

    0
    Text

    [/pre]

  3. A drive local to your database server. No, it also works for other operating systems (adjust your directory alias accordingly).

  4. Nice, but it raise some questions:

    What is a “Windows local drive”? A drive local to your database server? A drive local to your database client?
    Is the above code only working for Windows?

Comments are closed.

Next Post

SOA Training Program - Getting started with BPEL

Two weeks ago, during the first session of our 3-month SOA Training program, we worked very hard on XML (and XSD, XPath and XSLT). Today, this starts to pay off: we are entering the world of Business Processes and their implementation through the Business Process Execution Language, or BPEL for […]
%d bloggers like this: