Solved

How to loop through the children nodes of an XML table with Oracle?

Posted on 2014-11-14
1
949 Views
Last Modified: 2014-11-22
Hello,

I'm trying to figure out how to loop through multiple child nodes of the same name in an XML table; like this:

<root>
    <attributes>
        <people>
            <name>Joe</name>
            <interests>Sports</interes<wbr ></wbr>ts>
        </people>
        <people>
            <name>James</name>
            <interests>Video Games</interests>
        </people>
    </attributes>
</root>

Open in new window


I would like to get the list of people who are interested in Sports, but when I try to get the people attribute; I get an error saying the XML node had to be unique and wouldn't work unless I use the specific number; at which point it becomes a little frustrating when the node isn't always in the same place every time.

Is there any smarter way to loop through the Oracle XML table programmatically with a query?

Appreciate any help!
0
Comment
Question by:Errang Genevre
1 Comment
 
LVL 20

Accepted Solution

by:
flow01 earned 500 total points
ID: 40444348
You did not mention the  way  you were trying it.

after cleaning up
<interests>Sports</interes<wbr ></wbr>ts>

you can use an sql like this
  with your_xml
  as
  (
  select
  xmltype('
  <root>
    <attributes>
        <people>
            <name>Joe</name>
            <interests>Sports</interests>
        </people>
        <people>
            <name>James</name>
            <interests>Video Games</interests>
        </people>
    </attributes>
</root>
  ') xml_col
  from dual
  )
  select name, interests
  from your_xml,
  xmltable ('/root/attributes/people'
  passing(xml_col)
  columns name varchar2(200) path 'name',
          interests varchar2(4000) path 'interests'
  )
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Cursors in Oracle: A cursor is used to process individual rows returned by database system for a query. In oracle every SQL statement executed by the oracle server has a private area. This area contains information about the SQL statement and theā€¦
Read about achieving the basic levels of HRIS security in the workplace.
This video shows setup options and the basic steps and syntax for duplicating (cloning) a database from one instance to another. Examples are given for duplicating to the same machine and to different machines
This video explains what a user managed backup is and shows how to take one, providing a couple of simple example scripts.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now