convert oracle blob to xml type

假如想象 提交于 2019-12-05 04:51:05
user2464519
select
XMLType( BLOB_COLUMN,
         1 /* this is your character set ID.
                   1 == USASCII */
       ) as XML
from my_table;

For more character sets: http://www.mydul.net/charsets.html

You can convert from a BLOB to a CLOB and then pass the CLOB into the constructor of XMLTYPE. Here's a function...

-- PL/SQL function to convert a BLOB to an XMLTYPE
-- Usage: SELECT blob_to_xmltype(blob_column) FROM table_name;

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;
/

And for your specific example above you can use the EXTRACT() function:

SELECT extract(blob_to_xmltype(myColumn), '/ROOT/a') FROM table_name;

The above will return another XMLTYPE. If you want to get the text value of the node, you can use the EXTRACTVALUE() function instead.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!