XML DB - Introduction

Oracle allows XML documents to be stored in the database in three different ways:

The first page covers preparatory steps and also discusses and example that will be used in the following pages.


This pages uses an XML document containing a set of points representing the co-ordinates of a route (road, railway, canal etc).

For example the following is the XML document for the Great Orme Tramway in Llandudno, Wales

<?xml version="1.0"?>
  <title>Great Orme Tramway</title>
  <line id="1">

XML Schema

In order to use structured storage for the XML document in the Oracle database, it is necessary first to create an XML schema. The XML schema defines the structure of the document. All documents using this schema must comply to the specification. Effectively the XML schema defines the syntax and to some extent the semantics of the XML document.

Developing an XML schema from scratch would have been too time consuming, so I searched the internet and discovered http://www.freeformatter.com/xsd-generator.html which generates an XML schema from a sample XML document. The above document is sufficient to create an XML schema for these examples. It may be necessary to extend either the sample XML document or the generated XML schema to represent additional elements or attributes.

I reformatted the output from the XSD generator after I had cut-and-pasted it to my laptop.

The reformatted XML schema is as follows:

<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
  <xs:element name="route">
      <xs:sequence><xs:element name="title" type="xs:string"/>
        <xs:element name="centre" type="xs:string"/>
        <xs:element name="zoom" type="xs:byte"/>
        <xs:element name="line">
          <xs:element name="point" type="xs:string" minOccurs="0" maxOccurs="unbounded"/>
            <xs:attribute name="id" type="xs:byte"/>

As you would expect the XML schema reflects the structure of the original XML document.

So now we have an XML schema and an XML document, what can we do with it?


Fortunately I do not have to worry about security, so I created a user called XML and granted it DBA privileges. Obviously you may need to take additional security precautions in your environment.


Before you can access an XML document within XML DB, it must be created as a resource.

There are several ways to load XML documents. The simplest is to use a BFILE which is a variant of the LOB feature where the metadata is located within the database, but the document itself is stored in an external file system.

In this example we will store our documents in the /home/oracle/xdb directory. We will create an Oracle directory object called XDBDIR in the database for this directory:

CREATE OR REPLACE DIRECTORY xdbdir AS '/home/oracle/xdb';

All source documents for my examples (XML schema and XML data) will be located in the above directory.

In Oracle there is a default directory called XMLDIR with the following path:


For example:



XML DB stores documents within the database in a virtual folders structure. The virtual folders must exist before the documents can be created.

Virtual folders are created using the CREATEFOLDER function in DBMS_XDB. A parent folder must exist before the child folder is created.

In this example we will store schemas in /route/schema and data in /route/data. The folders are created as follows:

  res BOOLEAN;
  res := dbms_xdb.createfolder('/route/schema');  
  res := dbms_xdb.createfolder('/route/schema');
  res := dbms_xdb.createfolder('/route/data');

The XSL schema can be extended to include tags that specify how Oracle should store the document within the Oracle database. This allows more control of storage options e.g. specification of object names etc.

In order to include the XDB tags, we must modify the header of the XSL schema as follows:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"

There are two ways of storing array data in XML DB; using a VARRAY and using a nested table. In the second page in this series we will investigate VARRAY storage; in the third page we will cover nested tables.