The SQLXML interface provides methods for accessing the XML value as a String, a Reader or Writer, or as a Stream. The XML value may also be accessed through a Source or set as a Result, which are used with XML Parser APIs such as DOM, SAX, and StAX, as well as with XSLT transforms and XPath evaluations.
Methods in the interfaces ResultSet, CallableStatement, and PreparedStatement, such as getSQLXML allow a programmer to access an XML value. In addition, this interface has methods for updating an XML value.
The XML value of the SQLXML instance may be obtained as a BinaryStream using
SQLXML sqlxml = resultSet.getSQLXML(column); InputStream binaryStream = sqlxml.getBinaryStream();For example, to parse an XML value with a DOM parser:
DocumentBuilder parser = DocumentBuilderFactory.newInstance().newDocumentBuilder(); Document result = parser.parse(binaryStream);or to parse an XML value with a SAX parser to your handler:
SAXParser parser = SAXParserFactory.newInstance().newSAXParser(); parser.parse(binaryStream, myHandler);or to parse an XML value with a StAX parser:
XMLInputFactory factory = XMLInputFactory.newInstance(); XMLStreamReader streamReader = factory.createXMLStreamReader(binaryStream);
Because databases may use an optimized representation for the XML, accessing the value through getSource() and setResult() can lead to improved processing performance without serializing to a stream representation and parsing the XML.
For example, to obtain a DOM Document Node:
DOMSource domSource = sqlxml.getSource(DOMSource.class); Document document = (Document) domSource.getNode();or to set the value to a DOM Document Node to myNode:
DOMResult domResult = sqlxml.setResult(DOMResult.class); domResult.setNode(myNode);or, to send SAX events to your handler:
SAXSource saxSource = sqlxml.getSource(SAXSource.class); XMLReader xmlReader = saxSource.getXMLReader(); xmlReader.setContentHandler(myHandler); xmlReader.parse(saxSource.getInputSource());or, to set the result value from SAX events:
SAXResult saxResult = sqlxml.setResult(SAXResult.class); ContentHandler contentHandler = saxResult.getXMLReader().getContentHandler(); contentHandler.startDocument(); // set the XML elements and attributes into the result contentHandler.endDocument();or, to obtain StAX events:
StAXSource staxSource = sqlxml.getSource(StAXSource.class); XMLStreamReader streamReader = staxSource.getXMLStreamReader();or, to set the result value from StAX events:
StAXResult staxResult = sqlxml.setResult(StAXResult.class); XMLStreamWriter streamWriter = staxResult.getXMLStreamWriter();or, to perform XSLT transformations on the XML value using the XSLT in xsltFile output to file resultFile:
File xsltFile = new File("a.xslt");
File myFile = new File("result.xml");
Transformer xslt = TransformerFactory.newInstance().newTransformer(new StreamSource(xsltFile));
Source source = sqlxml.getSource(null);
Result result = new StreamResult(myFile);
xslt.transform(source, result);
or, to evaluate an XPath expression on the XML value:
XPath xpath = XPathFactory.newInstance().newXPath(); DOMSource domSource = sqlxml.getSource(DOMSource.class); Document document = (Document) domSource.getNode(); String expression = "/foo/@bar"; String barValue = xpath.evaluate(expression, document);To set the XML value to be the result of an XSLT transform:
File sourceFile = new File("source.xml");
Transformer xslt = TransformerFactory.newInstance().newTransformer(new StreamSource(xsltFile));
Source streamSource = new StreamSource(sourceFile);
Result result = sqlxml.setResult(null);
xslt.transform(streamSource, result);
Any Source can be transformed to a Result using the identity transform
specified by calling newTransformer():
Transformer identity = TransformerFactory.newInstance().newTransformer();
Source source = sqlxml.getSource(null);
File myFile = new File("result.xml");
Result result = new StreamResult(myFile);
identity.transform(source, result);
To write the contents of a Source to standard output:
Transformer identity = TransformerFactory.newInstance().newTransformer(); Source source = sqlxml.getSource(null); Result result = new StreamResult(System.out); identity.transform(source, result);To create a DOMSource from a DOMResult:
DOMSource domSource = new DOMSource(domResult.getNode());
Incomplete or invalid XML values may cause an SQLException when set or the exception may occur when execute() occurs. All streams must be closed before execute() occurs or an SQLException will be thrown.
Reading and writing XML values to or from an SQLXML object can happen at most once. The conceptual states of readable and not readable determine if one of the reading APIs will return a value or throw an exception. The conceptual states of writable and not writable determine if one of the writing APIs will set a value or throw an exception.
The state moves from readable to not readable once free() or any of the reading APIs are called: getBinaryStream(), getCharacterStream(), getSource(), and getString(). Implementations may also change the state to not writable when this occurs.
The state moves from writable to not writable once free() or any of the writing APIs are called: setBinaryStream(), setCharacterStream(), setResult(), and setString(). Implementations may also change the state to not readable when this occurs.
All methods on the SQLXML interface must be fully implemented if the
JDBC driver supports the data type.
HSQLDB-Specific Information:
Starting with HSQLDB 2.0, a rudimentary client-side SQLXML interface implementation (this class) is supported for local use when the product is built and run under JDK 1.6+ and the SQLXML instance is constructed as the result of calling JDBCConnection.createSQLXML().SQLXML instances retrieved in such a fashion are initially write-only, with the lifecycle of read and write availability constrained in accordance with the documentation of the interface methods.
When build and run under JDK 1.6+, it is also possible to retrieve read-only SQLXML instances from JDBCResultSet.getSQLXML(...), given that the underlying data can be converted to an XML Document Object Model (DOM).
However, at the time of this writing (2007-06-12) it is not yet possible to store SQLXML objects directly into an HSQLDB database or to use them directly for HSQLDB statement parameterization purposes. This is because the SQLXML data type is not yet natively supported by the HSQLDB engine. Instead, a JDBCSQLXML instance must first be read as a string, binary input stream, character input stream and so on, which can then be used for such purposes.
Here is the current read/write availability lifecycle for JDBCSQLXML:
| Origin | Initially | After 1st Write | After 1st Read | After 1st Free |
|---|---|---|---|---|
org.hsqldb.jdbc.JDBCConnection.createSQLXML()
|
Write-only | Read-only | Not readable or writable | Not readable or writable |
org.hsqldb.jdbc.JDBCResultSet.getSQLXML(...)
|
Read-only | N/A | Not readable or writable | Not readable or writable |
javax.xml.parsers , javax.xml.stream , javax.xml.transform , javax.xml.xpath
| Modifier and Type | Class and Description |
|---|---|
| protected static class | JDBCSQLXML.
Basically just a namespace to isolate SQLXML exception generation |
| protected static class | JDBCSQLXML.
Builds a DOM from SAX events. |
| public static class | JDBCSQLXML.
Writes to a |
| Modifier and Type | Field and Description |
|---|---|
| private JDBCSQLXML. | builder
When non-null, the SAX ContentHandler currently in use to build this object's SQLXML value from a SAX event sequence. |
| private boolean | closed
Whether this object is closed. |
| private static String | |
| private static DOMImplementation | |
| private static DOMImplementationRegistry | |
| private DOMResult | domResult
When non-null, the DOMResult currently in use to build this object's SQLXML value. |
| private static ThreadPoolExecutor | |
| private volatile byte[] | gzdata
This object's SQLXML value as a GZIPed byte array |
| private static Transformer | |
| private InputStream | inputStream
When non-null, the stream currently in use to read this object's SQLXML value as an octet sequence. |
| private ClosableByteArrayOutputStream | outputStream
When non-null, the stream currently in use to write this object's SQLXML value from an octet sequence. |
| private String | publicId
This object's public id |
| private boolean | readable
Whether it is possible to read this object's SQLXML value. |
| private String | systemId
This object's system id |
| private static TransformerFactory | |
| private static final Charset | utf8Charset
Precomputed Charset to reduce octet to character sequence conversion charset lookup overhead. |
| private static ArrayBlockingQueue | |
| private boolean | writable
Whether it is possible to write this object's SQLXML value. |
| Access | Constructor and Description |
|---|---|
| protected | |
| protected | JDBCSQLXML(byte[]
the octet sequence representing the SQLXML value bytes)Constructs a new read-only JDBCSQLXML object from the given octet sequence. |
| protected | JDBCSQLXML(char[]
the character sequence representing the SQLXML value chars)Constructs a new read-only JDBCSQLXML object from the given character sequence. |
| protected | JDBCSQLXML(Document
the Document representing the SQLXML value document)Constructs a new JDBCSQLXML object from the given Document. |
| protected | JDBCSQLXML(InputStream
an octet stream representing an SQLXML value inputStream)Constructs a new read-only JDBCSQLXML object from the given octet sequence. |
| protected | JDBCSQLXML(Reader
a character stream representing an SQLXML value reader)Constructs a new read-only JDBCSQLXML object from the given character sequence. |
| public | JDBCSQLXML(Source
a Source representing an SQLXML value source)Constructs a new read-only JDBCSQLXML object from the given Source object. |
| protected | JDBCSQLXML(String
a character sequence representing an SQLXML value string)Constructs a new read-only JDBCSQLXML object from the given character sequence. |
| protected | JDBCSQLXML(byte[]
the octet sequence representing the SQLXML value bytes, String must be a String that conforms to the URI syntax;
allows relative URIs to be processed. systemId)Constructs a new read-only JDBCSQLXML object from the given octet sequence. |
| protected | |
| protected | JDBCSQLXML(InputStream
an octet stream representing an SQLXML value inputStream, String a String that conforms to the URI syntax, indicating
the URI from which the XML data is being read, so that relative URI
references can be resolved systemId)Constructs a new read-only JDBCSQLXML object from the given octet sequence. |
| protected | JDBCSQLXML(Reader
a character stream representing an SQLXML value; reader, String a String that conforms to the URI syntax, indicating
the URI from which the XML data is being read, so that relative URI
references can be resolved systemId)Constructs a new read-only JDBCSQLXML object from the given character sequence. |
| protected | JDBCSQLXML(String
a character sequence representing an SQLXML value string, String a String that conforms to the URI syntax, indicating
the URI from which the XML data is being read, so that relative URI
references can be resolved systemId)Constructs a new read-only JDBCSQLXML object from the given character sequence. |
| protected | JDBCSQLXML(byte[]
the gzipped octet sequence representing the SQLXML value bytes, boolean whether to clone the given gzipped octet sequence clone, String id systemId, String id publicId)Constructs a new read-only JDBCSQLXML object from the given gzipped octet sequence. |
| protected |
| Modifier and Type | Method and Description |
|---|---|
| protected synchronized void | |
| protected synchronized void | |
| protected synchronized void | |
| protected synchronized void | close()
Overrides default java. closes this object and releases the resources that it holds. |
| protected static Document | createDocument(String
of the document element to create or namespaceURI, String null.of the document element to be created or qualifiedName, DocumentType null.of document to be created or docType)null.
When doctype is not null, its
Node.ownerDocument attribute is set to the document
being created.Returns: with its document element. |
| protected static Document | |
| protected <T extends Result> T | Returns: for setting the XML value designated by this SQLXML instance.The class of the result, or null. resultClass)Retrieves a new DOMResult for setting the XML value designated by this SQLXML instance. |
| protected <T extends Source> T | Returns: a new DOMSource for reading the XML value designated by this SQLXML instanceThe class of the source sourceClass)Retrieves a new DOMSource for reading the XML value designated by this SQLXML instance. |
| protected <T extends Result> T | Returns: for setting the XML value designated by this SQLXML instance.The class of the result, or null. resultClass)Retrieves a new Result for setting the XML value designated by this SQLXML instance. |
| protected <T extends Result> T | Returns: for setting the XML value designated by this SQLXML instance.The class of the result, or null. resultClass)Retrieves a new SAXResult for setting the XML value designated by this SQLXML instance. |
| protected <T extends Source> T | Returns: a new SAXSource for reading the XML value designated by this SQLXML instanceThe class of the source sourceClass)Retrieves a new SAXSource for reading the XML value designated by this SQLXML instance. |
| protected <T extends Result> T | Returns: for setting the XML value designated by this SQLXML instance.The class of the result, or null. resultClass)Retrieves a new DOMResult for setting the XML value designated by this SQLXML instance. |
| protected <T extends Source> T | Returns: a new StAXSource for reading the XML value designated by this SQLXML instanceThe class of the source sourceClass)Retrieves a new StAXSource for reading the XML value designated by this SQLXML instance. |
| protected <T extends Result> T | Returns: for setting the XML value designated by this SQLXML instance.The class of the result, or null. resultClass)Retrieves a new StreamResult for setting the XML value designated by this SQLXML instance. |
| protected <T extends Source> T | Returns: a new StreamSource for reading the XML value designated by this SQLXML instanceThe class of the source sourceClass)Retrieves a new StreamSource for reading the XML value designated by this SQLXML instance. |
| public void | free()
Implements java. This method closes this object and releases the resources that it held. |
| protected void | |
| protected void | freeInputStream()
Closes the input stream, if any, currently in use to read this object's SQLXML value and nullifies the stream reference to make it eligible for subsequent garbage collection. |
| protected void | freeOutputStream()
Closes the output stream, if any, currently in use to write this object's SQLXML value and nullifies the stream reference to make it eligible for subsequent garbage collection. |
| public synchronized InputStream | Returns: a stream containing the XML data.Implements java. Retrieves the XML value designated by this SQLXML instance as a stream. |
| protected InputStream | Returns: a stream containing the XML data.Retrieves a stream representing the XML value designated by this SQLXML instance. |
| public synchronized Reader | Returns: a stream containing the XML data.Implements java. Retrieves the XML value designated by this SQLXML instance as a java.io.Reader object. |
| protected Reader | Returns: a reader containing the XML data.Retrieves a reader representing the XML value designated by this SQLXML instance. |
| protected static DOMImplementation | |
| protected static DOMImplementationRegistry | |
| protected static ExecutorService | |
| protected byte[] | Returns: this object's SQLXML valueRetrieves this object's SQLXML value as a gzipped array of bytes, possibly by terminating any in-progress write operations and converting accumulated intermediate data. |
| protected static Transformer | |
| public synchronized <T extends Source> T | Returns: a Source for reading the XML value.The class of the source, or null.
If the class is null, a vendor specific Source implementation will be returned.
The following classes are supported at a minimum:
sourceClass)javax.xml.transform.dom.DOMSource - returns a DOMSource javax.xml.transform.sax.SAXSource - returns a SAXSource javax.xml.transform.stax.StAXSource - returns a StAXSource javax.xml.transform.stream.StreamSource - returns a StreamSource Implements java. Returns a Source for reading the XML value designated by this SQLXML instance. |
| protected <T extends Source> T | Returns: a Source for reading the XML value.The class of the source, or null. If null, then a
DOMSource is returned. sourceClass)Returns a Source for reading the XML value designated by this SQLXML instance. |
| public synchronized String | Returns: a string representation of the XML value designated by this SQLXML instance.Implements java. Returns a string representation of the XML value designated by this SQLXML instance. |
| protected String | Returns: a string containing the XML data.Retrieves a string representing the XML value designated by this SQLXML instance. |
| protected static TransformerFactory | |
| protected byte[] | Returns: this object's SQLMXL value as a gzipped byte arrayDirectly retrieves this object's present SQLMXL value as a gzipped array of bytes. |
| protected void | |
| public synchronized boolean | Returns: iftrue, then readable; else not readableRetrieves the object's readability status. |
| public synchronized boolean | Returns: iftrue, then writable; else not writableRetrieves the object's readability status. |
| public synchronized OutputStream | Returns: a stream to which data can be written.Implements java. Retrieves a stream that can be used to write the XML value that this SQLXML instance represents. |
| protected OutputStream | Returns: a stream to which the data can be written.Retrieves a stream to completely write the XML value this SQLXML instance represents. |
| public synchronized Writer | Returns: a stream to which data can be written.Implements java. Retrieves a stream to be used to write the XML value that this SQLXML instance represents. |
| protected Writer | Returns: a writer to which the data can be written.Retrieves a writer to completely write the XML value this SQLXML instance represents. |
| protected void | setGZipData(byte[]
the SQLXML value data)Assigns this object's SQLXML value from the designated gzipped array of bytes. |
| protected synchronized void | setReadable(boolean
if readable)true, then readable; else not readableAssigns this object's readability status. |
| public synchronized <T extends Result> T | Returns: Returns a Result for setting the XML value.The class of the result, or null.
If resultClass is null, a vendor specific Result implementation will be returned.
The following classes are supported at a minimum:
resultClass)javax.xml.transform.dom.DOMResult - returns a DOMResult javax.xml.transform.sax.SAXResult - returns a SAXResult javax.xml.transform.stax.StAXResult - returns a StAXResult javax.xml.transform.stream.StreamResult - returns a StreamResult Implements java. Returns a Result for setting the XML value designated by this SQLXML instance. |
| public synchronized void | setString(String
the XML value value)Implements java. Sets the XML value designated by this SQLXML instance to the given String representation. |
| protected void | setStringImpl(String
the XML value value)Sets the XML value designated by this SQLXML instance using the given String representation. |
| protected synchronized void | setWritable(boolean
if writable)true, then writable; else not writableAssigns this object's writability status. |