Is there a method in PL/SQL to convert/encode text to XML compliant text? Is there a method in PL/SQL to convert/encode text to XML compliant text? oracle oracle

Is there a method in PL/SQL to convert/encode text to XML compliant text?


Well, if you just want to convert XML characters, you'll want to do something like...

  outgoing_text := DBMS_XMLGEN.CONVERT(incoming_text)

Where outgoing_text and incoming_text are both VARCHAR2 or CLOB.

You can specify a second argument, but it defaults to DBMS_XMLGEN.ENTITY_ENCODE... it can also decode XML entities by passing DBMS_XMLGEN.ENTITY_DECODE as a second argument.


Later versions of oracle have a built in XML package for manipulating XML data.
For example, is this the sort of thing your colleague wants to do?:

SELECT DBMS_XMLGEN.getXML('SELECT * FROM emp') FROM dual;


In addition to dbms_xmlgen, you can use the sql method, xmlelement and then extract the value back out.

select extract(xmlelement("mytest",my_variable),'mytest/text()') from dual;

The xmlelement function will make the text XML compliant, then using the extract function, it will extract the text out as is. (Note: The extractValue function will convert the text back to the non-XML compliant version.).