Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Oracle Pl/SQL: Loop through XMLTYPE nodes

I have a XMLTYPE with the following content:

<?xml version="1.0"?>
    <users>
        <user>
            <name>user1</name>
        </user>
        <user>
            <name>user2</name>
        </user>
        <user>
            <name>user3</name>
        </user>
    </users>

How can I loop in PL/SQL through all the elements "user"? Thanks

like image 842
pistacchio Avatar asked Jun 12 '09 09:06

pistacchio


People also ask

How do I display XMLType data in SQL Developer?

There is a setting in the preferences to let it show the actual XML in the Query Results window. Tick 'Display XML Value in Grid', click OK, and re-run your query.

What is the use of @XMLType?

@XmlType allows you to specify a factory class and/or method that can be used to instantiate the domain object instead of the default constructor.

What is XMLElement in SQL?

XMLElement takes an element name for identifier , an optional collection of attributes for the element, and arguments that make up the content of the element. It returns an instance of type XMLType .

What is @XMLType?

XMLType is a system-defined opaque type for handling XML data. It as predefined member functions on it to extract XML nodes and fragments. You can create columns of XMLType and insert XML documents into it.


3 Answers

You can loop through the elements using EXTRACT and XMLSequence (splits the XML into distinct chunks -- here users) like this:

SQL> SELECT extractvalue(column_value, '/user/name') "user"   2    FROM TABLE(XMLSequence(XMLTYPE(   3                 '<?xml version="1.0"?>   4                     <users>   5                         <user>   6                             <name>user1</name>   7                         </user>   8                         <user>   9                             <name>user2</name>  10                         </user>  11                         <user>  12                             <name>user3</name>  13                         </user>  14                     </users>').extract('/users/user'))) t;  user -------- user1 user2 user3 
like image 121
Vincent Malgrat Avatar answered Sep 23 '22 12:09

Vincent Malgrat


You can use XQuery. Check out the select statement below. v_xml_doc is the XMLTYPE variable containing the XML data.

select name from   XMLTable('for $i in /users/user                             return $i'                             passing   v_xml_doc                             columns   name varchar2(200) path 'name'                ) 
like image 26
diederikh Avatar answered Sep 26 '22 12:09

diederikh


How about this:

PROCEDURE xmltest IS
  v_userlist XMLType;
  v_count NUMBER(38) := 1;
BEGIN
  /* define XML variable */
  v_userlist := XMLType('<?xml version="1.0"?>
    <users>
        <user>
            <name>user1</name>
        </user>
        <user>
            <name>user2</name>
        </user>
        <user>
            <name>user3</name>
        </user>
    </users>');

  /* for each user, print out their name (each element can be extracted using xpath '//user[1]' '//user[2]' etc) */
  WHILE v_userlist.existsNode('//user[' || v_count || ']') = 1 LOOP
    dbms_output.put_line(v_userlist.extract('//user[' || v_count || ']/name/text()').getStringVal());
    v_count := v_count + 1;
  END LOOP;
END;
like image 23
Fergus McGlynn Avatar answered Sep 26 '22 12:09

Fergus McGlynn