1

I am trying to parse a huge XML CLOB data to get some node values. So I wrote a method with the help of DBMS_XMLPARSER to retrieve a value of a node and it works fine.

FUNCTION Get_Node_Value(
      node_ IN DBMS_XMLDOM.DOMNode) RETURN VARCHAR2
   IS
      sub_nodes_list_  DBMS_XMLDOM.DOMNodeList;
      sub_nodes_len_   NUMBER;
      value_node_      DBMS_XMLDOM.DOMNode;
      value_           VARCHAR2(50);
   BEGIN
      sub_nodes_list_ := DBMS_XMLDOM.Getchildnodes(node_); 
      sub_nodes_len_  := DBMS_XMLDOM.GetLength(sub_nodes_list_); -- sub_nodes_len_ should be 1
      IF ( sub_nodes_len_ > 0 ) THEN
            value_node_ := DBMS_XMLDOM.Item(sub_nodes_list_, 0);
            value_      := DBMS_XMLDOM.GetNodeValue(value_node_);
      END IF;      
      RETURN value_;
   END Get_Node_Value;

But I am wondering to have a common method where I give XML CLOB and node name as parameters and get node value return back or something relevant. Since the XML is too large, using such a method will cut down unnecessary coding. Any help will be appreciated!

1 Answer 1

1

Xquery is a query and "programing" language designed to work with xml document. Oracle has two main function which accept xquery commands. xmltable and xmlquery. usign xmltable and xmlquery

Short example.

select * from xmltable('/ROWSET/ROW/TABLE_NAME/text()' passing xmltype('<?xml version="1.0"?>
                                    <ROWSET>
                                     <ROW>
                                      <TABLE_NAME>DBA_2PC_NEIGHBORS</TABLE_NAME>
                                      <COMMENTS>information about incoming and outgoing connections for pending transactions</COMMENTS>
                                     </ROW>
                                     <ROW>
                                      <TABLE_NAME>DBA_2PC_PENDING</TABLE_NAME>
                                      <COMMENTS>info about distributed transactions awaiting recovery</COMMENTS>
                                     </ROW>
                                     <ROW>
                                      <TABLE_NAME>DBA_ADDM_FDG_BREAKDOWN</TABLE_NAME>
                                     </ROW>
                                    </ROWSET>') )
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks. Since I have XML message is in BLOB it seems difficult to use Xquery in PL/SQL code! errors are like "XML parse failure" any suggestions?
How are you trying to convert clob into xmltype?
Actually XML it is BLOB so I convert it to CLOB using DBMS_LOB.converttoclob method. Then using DBMS_XMLPARSER.parseclob() and DBMS_XMLPARSER.getDocument() methods to create DBMS_XMLDOM.DOMDocument. After that only I am using DBMS_XMLDOM package to parse the XML and retrieving the node value.
Try xmltype. One version of constructor accepts blob, and encoding chareset_id. charset id for NLS_CHARSET_ID('AL32UTF8')`. For more charesets check link

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.