The DBMS_XMLGEN
package converts the results of a SQL query to a canonical XML format. The package takes an arbitrary SQL query as input, converts it to XML format, and returns the result as a CLOB
. This package is similar to the DBMS_XMLQUERY
package, except that it is written in C and compiled into the kernel. This package can only be run on the database.
This chapter contains the following topic:
See Also:
Oracle XML DB Developer's Guide, for more information on XML support and on examples of usingDBMS_XMLGEN
Table 172-1 Summary of DBMS_XMLGEN Package Subprograms
Subprogram | Description |
---|---|
Closes the context and releases all resources |
|
Converts the XML into the escaped or unescaped XML equivalent |
|
Gets the number of SQL rows that were processed in the last call to GETXML Functions |
|
Gets the XML document |
|
Gets the XML document and returns it as |
|
Creates a new context handle |
|
Obtains a handle to use in the GETXML Functions and other functions to get a hierarchical XML with recursive elements from the result |
|
Restarts the query to start fetching from the beginning |
|
Sets whether special characters such as |
|
Sets the maximum number of rows to be fetched each time |
|
Sets |
|
Sets the name of the element enclosing the entire result |
|
Sets the name of the element enclosing each row of the result |
|
Sets the number of rows to skip every time before generating the XML. |
|
Forces the use of the collection column name appended with the tag |
|
Specified whether to use an XML attribute to indicate |
This procedure closes a given context and releases all resources associated with it, including the SQL cursor and bind and define buffers. After this call, the handle cannot be used for a subsequent function call.
This function converts the XML data into the escaped or unescapes XML equivalent, and returns XML CLOB
data in encoded or decoded format. There are several version of the function.
Uses XMLDATA
in string form (VARCHAR2
):
DBMS_XMLGEN.CONVERT ( xmlData IN VARCHAR2, flag IN NUMBER := ENTITY_ENCODE) RETURN VARCHAR2;
Uses XMLDATA
in CLOB
form:
DBMS_XMLGEN.CONVERT ( xmlData IN CLOB, flag IN NUMBER := ENTITY_ENCODE) RETURN CLOB;
This function retrieves the number of SQL rows processed when generating the XML using the GETXML Functions call. This count does not include the number of rows skipped before generating the XML. Note that GETXML Functions always generates an XML document, even if there are no rows present.
Table 172-4 GETNUMROWSPROCESSED Function Parameters
Parameter | Description |
---|---|
|
The context handle obtained from the NEWCONTEXT Functions call. |
This function is used to determine the terminating condition if calling GETXML Functions in a loop.
This function gets the XML document. The function is overloaded.
Gets the XML document by fetching the maximum number of rows specified. It appends the XML document to the CLOB
passed in. Use this version of GETXML Functions to avoid any extra CLOB
copies and to reuse the same CLOB
for subsequent calls. Because of the CLOB
reuse, this GETXML Functionscall is potentially more efficient:
DBMS_XMLGEN.GETXML ( ctx IN ctxHandle, tmpclob IN OUT NCOPY CLOB, dtdOrSchema IN number := NONE) RETURN BOOLEAN;
Generates the XML document and returns it as a temporary CLOB
. The temporary CLOB
obtained from this function must be freed using the DBMS_LOB.FREETEMPORARY
call:
DBMS_XMLGEN.GETXML ( ctx IN ctxHandle, dtdOrSchema IN number := NONE) RETURN CLOB;
Converts the results from the SQL query string to XML format, and returns the XML as a temporary CLOB
, which must be subsequently freed using the DBMS_LOB.FREETEMPORARY
call:
DBMS_XMLGEN.GETXML ( sqlQuery IN VARCHAR2, dtdOrSchema IN number := NONE) RETURN CLOB;
When the rows indicated by the SETSKIPROWS Procedure call are skipped, the maximum number of rows as specified by the SETMAXROWS Procedure call (or the entire result if not specified) is fetched and converted to XML. Use the GETNUMROWSPROCESSED Function to check if any rows were retrieved.
This function gets the XML document and returns it as an XMLTYPE
. XMLTYPE
operations can be performed on the results.This function is overloaded.
Generates the XML document and returns it as a sys.XMLType:
DBMS_XMLGEN.GETXMLTYPE ( ctx IN ctxhandle, dtdOrSchema IN number := NONE) RETURN sys.XMLType;
Converts the results from the SQL query string to XML format, and returns the XML as a sys.XMLType
:
DBMS_XMLGEN.GETXMLTYPE ( sqlQuery IN VARCHAR2, dtdOrSchema IN number := NONE) RETURN sys.XMLType
This function generates and returns a new context handle. This context handle is used in GETXML Functions and other functions to get XML back from the result. There are several version of the function.
Generates a new context handle from a query:
DBMS_XMLGEN.NEWCONTEXT ( query IN VARCHAR2) RETURN ctxHandle;
Generates a new context handle from a query string in the form of a PL/SQL ref cursor:
DBMS_XMLGEN.NEWCONTEXT ( queryString IN SYS_REFCURSOR) RETURN ctxHandle;
This function obtains a handle to use in the GETXML Functions and other functions to get a hierarchical XML with recursive elements from the result.
Table 172-8 NEWCONTEXTFROMHIERARCHY Function Parameters
Parameter | Description |
---|---|
|
The query string, the result of which must be converted to XML. The query is a hierarchical query typically formed using a |
This procedure restarts the query and generates the XML from the first row. It can be used to start executing the query again, without having to create a new context.
This procedure sets whether or not special characters in the XML data must be converted into their escaped XML equivalent. For example, the <
sign is converted to <.
The default is to perform conversions. This function improves performance of XML processing when the input data cannot contain any special characters such as <
, >
, ",'
, which must be escaped. It is expensive to scan the character data to replace the special characters, particularly if it involves a lot of data.
Table 172-10 SETCONVERTSPECIALCHARS Procedure Parameters
Parameter | Description |
---|---|
|
The context handle obtained from one of the NEWCONTEXT Functions call. |
|
|
This procedure sets the maximum number of rows to fetch from the SQL query result for every invokation of the GETXML Functions call. It is used when generating paginated results. For example, when generating a page of XML or HTML data, restrict the number of rows converted to XML or HTML by setting the maxrows
parameter.
Table 172-11 SETMAXROWS Procedure Parameters
Parameter | Description |
---|---|
|
The context handle corresponding to the query executed. |
|
The maximum number of rows to get for each call to GETXML Functions |
This procedure sets NULL
handling options, handled through the flag
parameter setting.
Table 172-12 SETNULLHANDLING Procedure Parameters
Parameter | Description |
---|---|
|
The context handle corresponding to the query executed. |
|
The
|
This procedure sets the name of the root element of the document. The default name is ROWSET.
Table 172-13 SETROWSETTAG Procedure Parameters
Parameter | Description |
---|---|
ctx |
The context handle obtained from the NEWCONTEXT Functions call. |
rowSetTagName |
The name of the document element. Passing |
The user can set the rowSetTag
to NULL
to suppress the printing of this element. However, an error is produced if both the row and the rowset are NULL
and there is more than one column or row in the output . This is because the generated XML would not have a top-level enclosing tag, and so would be invalid.
This procedure sets the name of the element separating all the rows. The default name is ROW.
Table 172-14 SETROWTAG Procedure Parameters
Parameter | Description |
---|---|
|
The context handle obtained from the NEWCONTEXT Functions call. |
|
The name of the |
The user can set the name of the element to NULL
to suppress the ROW
element itself. However, an error is produced if both the row and the rowset are NULL
and there is more than one column or row in the output. This is because the generated XML would not have a top-level enclosing tag, and so would be invalid.
This procedure skips a given number of rows before generating the XML output for every call to the GETXML Functions. It is used when generating paginated results for stateless Web pages using this utility. For example, when generating the first page of XML or HTML data, set skiprows
to zero. For the next set, set the skiprows
to the number of rows obtained in the first case. See GETNUMROWSPROCESSED Function.
This procedure overrides the default name of the collection elements. The default name for collection elements is the type name itself.
This procedure specifies whether to use an XML attribute to indicate NULL
ness, or to do it by omitting the inclusion of the particular entity in the XML document. It is used as a shortcut for the SETNULLHANDLING Procedure.