Question: I need to Convert Data
stored [ Blob Type ] column to [ XML TYPE ] column in a Table
and extract the XML data from the XML TYPE column. How
do I convert a BLOB (CLOB) to an XML type?
Answer: The two methods below to convert BLOB
into XML types reply on the utl_raw.cast_to_varchar2 to make the
conversion from a BLOB to an XML type:
Convert CLOB to XML:
If you first
convert BLOB to CLOB you can use the following PL/SQL to
convert a CLOB to an XML type.
The following PL/SQL stored
procedure will accept a blob column as input and return an
XML data type.
Here is yet another way to convert a
CLOB to an XML string:
CREATE OR
REPLACE FUNCTION blob_to_xmltype (blob_in IN BLOB)
RETURN XMLTYPE
AS
v_clob CLOB;
v_varchar VARCHAR2(32767);
v_start PLS_INTEGER := 1;
v_buffer PLS_INTEGER := 32767;
BEGIN
DBMS_LOB.CREATETEMPORARY(v_clob, TRUE);
FOR i IN 1..CEIL(DBMS_LOB.GETLENGTH(blob_in)
/ v_buffer)
LOOP
v_varchar
:= UTL_RAW.CAST_TO_VARCHAR2(DBMS_LOB.SUBSTR(blob_in,
v_buffer, v_start));
DBMS_LOB.WRITEAPPEND(v_clob, LENGTH(v_varchar),
v_varchar);
v_start := v_start + v_buffer;
END LOOP;
RETURN XMLTYPE(v_clob);
END blob_to_xmltype;
;
You can invoke this function as follows:
SELECT extract(blob_to_xmltype(my_column_name),
'/tmp/xxx') FROM table_name;
You can also try this way to convert a
BLOB to an XML type:
select
XMLType( blob_datatype_column,
1 /* this is your character set ID. where 1 = USASCII */
)
as XML
from
my_table;