Reputation: 171
Suppose my DB column which is of datatype varchar2 has values that containcharacters like '<' '>' and '&' and i want them to be escaped like & lt; , & gt; etc when obtained using select query on these columns , is there some built-in feature in oracle , jdbc to perform this activity ?
Upvotes: 1
Views: 7961
Reputation: 191560
You can use the dbms_xmlgen.convert()
function:
select dbms_xmlgen.convert('<element>Text with "quotes" and ''&''</element>')
from dual;
DBMS_XMLGEN.CONVERT('<ELEMENT>TEXTWITH"QUOTES"AND''&''</ELEMENT>')
---------------------------------------------------------------------------------
<element>Text with "quotes" and '&'</element>
There are overloaded versions for varchar2
and clob
values. Just use your table column instead of the fixed string. Or convert to an XMLType variable which wil encode automatically, or use another XMLGen function if that's appropriate for your data and required output.
Upvotes: 3
Reputation: 17920
htf.escape_sc()
function do what you expect. But I would rather prefer doing this with Java itself.
SQL> select htf.escape_sc('<Welcome>Hello WOrld</Welcome>') parsed FROM DUAL;
PARSED
--------------------------------------------------------------------------------
<Welcome>Hello WOrld</Welcome>
Biggest Limitation is.. It is restricted to Maximum of 4000 charcters(after parsing) for VARCHAR
column.. And it threw an exception if it exceeds!! So, Please have these parsing in Client side.
Upvotes: 3