I have varchar2 column in DB. And this column stores string in xml format. But not all strings are well-formed xml (some with mistakes). How can I check is this string well-formed xml or not?
If this string isn't well formed xml, sql-query like this will fail at runtime:
select
extractvalue(xmltype(some_table.value), 'Attachment/@category')
from some_table
Xml has following format:
<Attachment {attributes} />
Number of attributes in each string can be different.
So when the string like 'asdf' would occur my query wouldn't fail.
Here's a simple function that will check it for you;
CREATE OR REPLACE FUNCTION isXML(xml CLOB)
RETURN NUMBER
AS
xmldata XMLTYPE;
BEGIN
xmldata := XMLTYPE(xml);
return 1;
EXCEPTION
when others then
return 0;
END;
/
You can use it as;
SQL> SELECT isXML('fdjkasksdf') FROM DUAL;
ISXML('FDJKASKSDF')
-------------------
0
SQL> SELECT isXML('<body></body>') FROM DUAL;
ISXML('<BODY></BODY>')
----------------------
1
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With