Solved

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

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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Cassandra Select Query 1 73
How Resolve Word 2011 Office-Database Error in Mac OS X 10 56
append to an ms access field 6 33
Oracle DB monitor SW 21 47
This article describes some very basic things about SQL Server filegroups.
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
This video explains what a user managed backup is and shows how to take one, providing a couple of simple example scripts.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

816 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

13 Experts available now in Live!

Get 1:1 Help Now