Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

xml - 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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...