CREATE OR REPLACE FUNCTION idAttributeOfDocElement(xmldoc VARCHAR2 ) RETURN VARCHAR2 IS theXmlDoc xmldom.DOMDocument; theDocElt xmldom.DOMElement; retval VARCHAR2(400); XMLParseError EXCEPTION; PRAGMA EXCEPTION_INIT( XMLParseError, -20100 ); -- Local parse function keeps code cleaner. Return NULL if parse fails FUNCTION parse(xml VARCHAR2) RETURN xmldom.DOMDocument IS retDoc xmldom.DOMDocument; parser xmlparser.Parser; BEGIN parser := xmlparser.newParser; xmlparser.parseBuffer(parser,xml); retDoc := xmlparser.getDocument(parser); xmlparser.freeParser(parser); RETURN retdoc; EXCEPTION -- If the parse fails, we'll jump here. WHEN XMLParseError THEN xmlparser.freeParser(parser); RETURN retdoc; END; BEGIN -- Parse the xml document passed in the VARCHAR2 argument theXmlDoc := parse(xmldoc); -- If the XML document returned is not NULL... IF NOT xmldom.IsNull(theXmlDoc) THEN -- Get the outermost enclosing element (aka "Document Element") theDocElt := xmldom.getDocumentElement(theXmlDoc); -- Get the value of the document element's "id" attribute retval := xmldom.getAttribute(theDocElt,'id'); -- Free the memory used by the parsed XML document xmldom.freeDocument(theXmlDoc); RETURN retval; ELSE RETURN NULL; END IF; END; |