The XML data type and operators are defined only in the SQL layer.
There is no JDBC-side support for XML data types. It is not possible to
bind directly into an XML value or to retrieve an XML value directly from
a result set. Instead, you must bind and retrieve the XML data as Java strings
or character streams by explicitly specifying the appropriate XML operator
as part of the SQL statements:
- Create a table with a XML data typed column. For example:
CREATE TABLE xml_data(xml_col XML);
- Use the XMLPARSE operator for binding data into XML values. For example:
INSERT INTO xml_data(xml_col)
VALUES(XMLPARSE(DOCUMENT ' <name> Derby </name>' PRESERVE WHITESPACE));
You must insert the XML keywords DOCUMENT and PRESERVE WHITESPACE.
Actual XML data should be inside single quotation marks, and
values should be within the starting XML tag and the ending XML tag.
- Use the XMLSERIALIZE operator to retrieve XML values from a result set.
For example:
SELECT XMLSERIALIZE(xml_col AS CLOB) FROM xml_data;
You can also specify xml_col AS VARCHAR(25).
- Use non-XML data retrieved from a non-XML column to create an XML fragment.
For example: SELECT '<my_self>' ||
'<name>'|| my_name ||'</name>' ||
'<age>'|| TRIM(CHAR(my_age)) ||'</age>'||
'</my_self>'
FROM my_non_xml_table;
This will result in XML fragments, which you must plug
into an XML document.
Additionally, there is no JDBC metadata support for the XML data type.
The XML data type is not allowed in any of the clauses or operations that
are described in the section on expressions on LONG data types in
.
For the XML operators to work properly, requires
that a JAXP parser, such as Apache Xerces, and an implementation of
the DOM Level 3 XPath specification, such as Apache Xalan, are
included in the Java classpath. If either the parser or the XPath
processor is missing from the classpath,
disallows any XML-related
operations.
Classpath and version issues
Most Java Virtual Machines (JVMs) that are version 1.6 or later,
have the required libraries embedded in the JVM. If you are using one
of these JVMs, you may not need to add any classes to your classpath.
If your JVM does not include the required libraries, you must add
Apache Xalan, or some other library that implements the required
functionality, to your classpath.
Some JVMs include a version of Xalan that is not new enough. If your
JVM comes with a too old version of Xalan, you may need to override
the version of Xalan in the JVM with a
newer version by using the Endorsed Standards Override Mechanism described at
http://download.oracle.com/javase/1.4.2/docs/guide/standards/. To
use this mechanism, download and install a binary distribution of Xalan from
Apache and set the system property java.endorsed.dirs to point
to the Xalan installation directory.