Example 5-2: Checking the well-formedness of XML in a CLOB

CREATE OR REPLACE PROCEDURE CheckXMLInCLOB(c CLOB, 
                                           wellFormed OUT BOOLEAN,
                                           error      OUT VARCHAR2) IS
  parser        xmlparser.Parser;
  xmldoc        xmldom.DOMDocument;
  XMLParseError EXCEPTION;

  -- Associate the XMLParseError exception with the -20100 error code
  PRAGMA EXCEPTION_INIT( XMLParseError, -20100 );

BEGIN

  -- (1) Create a new parser
  parser := xmlparser.newParser;

  -- (2) Attempt to parse the XML document in the CLOB
  xmlparser.ParseCLOB(parser,c);

  -- (3) Free the parser.
  xmlparser.freeParser(parser);

  -- If the parse succeeds, we'll get here
  wellFormed := TRUE;
EXCEPTION
  -- If the parse fails, we'll jump here.
  WHEN XMLParseError THEN
    xmlparser.freeParser(parser);
    wellFormed := FALSE;
    error := SQLERRM;
END;