CREATE OR REPLACE FUNCTION idAttributeOfDocElements(xmldoc in CLOB)
return varchar2
is
theXmlDoc xmldom.DOMDocument;
n1 xmldom.DOMNodeList;
len1 number(10);
len2 number(10);
v1 xmldom.DOMNode;
n2 xmldom.DOMNodeList;
attn xmldom.DOMNode;
vretuval varchar2(1000):='';
XMLParseError EXCEPTION;
PRAGMA EXCEPTION_INIT( XMLParseError, -20100 );
-- Local parse function keeps code cleaner. Return NULL if parse fails
FUNCTION parse(xml CLOB) RETURN xmldom.DOMDocument IS
retDoc xmldom.DOMDocument;
parser xmlparser.Parser;
BEGIN
parser := xmlparser.newParser;
xmlparser.ParseCLOB(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);
dbms_output.put_line('errors');
RETURN retdoc;
END;
BEGIN
-- Parse the xml document passed in the CLOB 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
n1:= xmldom.getElementsByTagName(theXmlDoc, 'doc');
len1 := xmldom.getLength(n1);
dbms_output.put_line('節點的數量:'||len1);
--獲得<HB></HB>節點的數量
for i in 0 .. len1 - 1 loop
--獲得節點
v1 := xmldom.item(n1, i);
--獲得該節點下所有的子節點
n2 := xmldom.getChildNodes(v1);
--獲得子節點的數量
len2 := xmldom.getLength(n2);
dbms_output.put_line('第-------------< '|| (i+1)|| ' >---------節點');
for j in 0..len2-1 loop
--dbms_output.put_line(len2);
attn:=xmldom.item(n2,j);
dbms_output.put_line(xmldom.getNodeValue(xmldom.getFirstChild(attn.id))||':'||xmldom.getNodeValue(xmldom.getFirstChild(attn)));
end loop;
end loop;
-- Free the memory used by the parsed XML document
xmldom.freeDocument(theXmlDoc);
RETURN vretuval;
ELSE
RETURN vretuval;
END IF;
END;
代碼不是我寫的,求大神說一下怎么去獲取節點屬性的值
uj5u.com熱心網友回復:
oracle本身有xmltype型別,把引數xmldoc轉換成xmltype,然后用extract很簡單的回傳對應節點的值轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/94862.html
標籤:開發
