Solved

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

Posted on 2014-11-14
1
1,066 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

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes some very basic things about SQL Server filegroups.
CCModeler offers a way to enter basic information like entities, attributes and relationships and export them as yEd or erviz diagram. It also can import existing Access or SQL Server tables with relationships.
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 shows how to configure and send email from and Oracle database using both UTL_SMTP and UTL_MAIL, as well as comparing UTL_SMTP to a manual SMTP conversation with a mail server.

821 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