Solved

Parsing XML held in a Clob field

Posted on 2008-11-03
7
1,783 Views
Last Modified: 2013-12-19
Hi All

I have some CLOB Data which is in XML format that i want to parse and process into tables within my database on my Oracle 10g database

Does anyone have an example script of how this is done?

Example XML:

<EXAMPLE>
      <CANDIDATES>
            <CANDIDATE>
                  <NAME>JAMES</NAME>
                  <JOBHISTORY>
                        <COMPANYNAME>A</COMPANYNAME>
                        <STARTDATE>01/01/2008</STARTDATE>
                        <ENDDATE>01/01/2008</ENDDATE>
                  </JOBHISTORY>
                  <JOBHISTORY>
                        <COMPANYNAME>B</COMPANYNAME>
                        <STARTDATE>01/01/2008</STARTDATE>
                        <ENDDATE>01/01/2008</ENDDATE>
                  </JOBHISTORY>
                  <JOBHISTORY>
                        <COMPANYNAME>C</COMPANYNAME>
                        <STARTDATE>01/01/2008</STARTDATE>
                        <ENDDATE>01/01/2008</ENDDATE>
                  </JOBHISTORY>
                  <EDUCATION>
                        <SCHOOLNAME>D</SCHOOLNAME>
                        <STARTDATE>01/01/2008</STARTDATE>
                        <ENDDATE>01/01/2008</ENDDATE>
                        <QUALIFICATIONS>
                              <SUBJECT>HISTORY<GRADE>A</GRADE>
                              </SUBJECT>
                              <SUBJECT>MATHS<GRADE>A</GRADE>
                              </SUBJECT>
                              <SUBJECT>ENGLISH<GRADE>A</GRADE>
                              </SUBJECT>
                              <SUBJECT>SCIENCE<GRADE>A</GRADE>
                              </SUBJECT>
                        </QUALIFICATIONS>
                  </EDUCATION>
            </CANDIDATE>
            <CANDIDATE>
                  <NAME>BOB</NAME>
                  <JOBHISTORY>
                        <COMPANYNAME>A</COMPANYNAME>
                        <STARTDATE>01/01/2008</STARTDATE>
                        <ENDDATE>01/01/2008</ENDDATE>
                  </JOBHISTORY>
                  <JOBHISTORY>
                        <COMPANYNAME>B</COMPANYNAME>
                        <STARTDATE>01/01/2008</STARTDATE>
                        <ENDDATE>01/01/2008</ENDDATE>
                  </JOBHISTORY>
                  <JOBHISTORY>
                        <COMPANYNAME>C</COMPANYNAME>
                        <STARTDATE>01/01/2008</STARTDATE>
                        <ENDDATE>01/01/2008</ENDDATE>
                  </JOBHISTORY>
                  <EDUCATION>
                        <SCHOOLNAME>D</SCHOOLNAME>
                        <STARTDATE>01/01/2008</STARTDATE>
                        <ENDDATE>01/01/2008</ENDDATE>
                        <QUALIFICATIONS>
                              <SUBJECT>HISTORY<GRADE>A</GRADE>
                              </SUBJECT>
                              <SUBJECT>MATHS<GRADE>A</GRADE>
                              </SUBJECT>
                              <SUBJECT>ENGLISH<GRADE>A</GRADE>
                              </SUBJECT>
                              <SUBJECT>SCIENCE<GRADE>A</GRADE>
                              </SUBJECT>
                        </QUALIFICATIONS>
                  </EDUCATION>
            </CANDIDATE>
      </CANDIDATES>
</EXAMPLE>


Many Thanks

Anthony
0
Comment
Question by:oogooglies
  • 4
  • 3
7 Comments
 
LVL 73

Expert Comment

by:sdstuber
ID: 22868455
Here's an example showing how to "flatten" the xml hierarchy which should allow you to insert into any number of target tables.
SELECT EXTRACTVALUE(VALUE(c), '/CANDIDATE/NAME') name,

       EXTRACTVALUE(VALUE(j), '/JOBHISTORY/COMPANYNAME') companyname,

       EXTRACTVALUE(VALUE(j), '/JOBHISTORY/STARTDATE') startdate,

       EXTRACTVALUE(VALUE(j), '/JOBHISTORY/ENDDATE') enddate

  FROM (SELECT xmltype(yourclob) xml FROM yourtable),

       table(XMLSEQUENCE(EXTRACT(xml, '/EXAMPLE/CANDIDATES/CANDIDATE'))) c,

       table(XMLSEQUENCE(EXTRACT(VALUE(c), '/CANDIDATE/JOBHISTORY'))) j

Open in new window

0
 

Author Comment

by:oogooglies
ID: 22871214
Thanks, is there any way to loop through the xml and store the data into a table.
0
 
LVL 73

Expert Comment

by:sdstuber
ID: 22872392
don't loop, just insert directly from the select statement


insert into your_table(name,companyname,startdate,enddate)
SELECT EXTRACTVALUE(VALUE(c), '/CANDIDATE/NAME') name,
       EXTRACTVALUE(VALUE(j), '/JOBHISTORY/COMPANYNAME') companyname,
       EXTRACTVALUE(VALUE(j), '/JOBHISTORY/STARTDATE') startdate,
       EXTRACTVALUE(VALUE(j), '/JOBHISTORY/ENDDATE') enddate
  FROM (SELECT xmltype(yourclob) xml FROM yourtable),
       table(XMLSEQUENCE(EXTRACT(xml, '/EXAMPLE/CANDIDATES/CANDIDATE'))) c,
       table(XMLSEQUENCE(EXTRACT(VALUE(c), '/CANDIDATE/JOBHISTORY'))) j
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 73

Expert Comment

by:sdstuber
ID: 22872399
oops,  "yourtable"  in the insert would be a different table than the "yourtable" in the select
0
 

Author Comment

by:oogooglies
ID: 22874291
Thanks for your help so far, the XML i am trying to parse is alot more complex than the exmaple i gave. is there anyway of getting oracle to create and populate tables in the structure of the XML?
0
 

Author Comment

by:oogooglies
ID: 22874352
Is there anyway oracle can dynamically go through the elements of XML data and depending on the element found process the data accordingly.
0
 
LVL 73

Accepted Solution

by:
sdstuber earned 500 total points
ID: 22876149
Sounds like you are trying to do XML Schema registration

Look at

DBMS_XMLSCHEMA  and the Packages and Types reference as well as the XML DB Reference

Also, you can do complex XQuery and XPath queries,  so even if the example is simplified, the concept is the same.
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Join & Write a Comment

Why doesn't the Oracle optimizer use my index? Querying too much data Most Oracle developers know that an index is useful when you can use it to restrict your result set to a small number of the total rows in a table. So, the obvious side…
Working with Network Access Control Lists in Oracle 11g (part 2) Part 1: http://www.e-e.com/A_8429.html Previously, I introduced the basics of network ACL's including how to create, delete and modify entries to allow and deny access.  For many…
This video shows information on the Oracle Data Dictionary, starting with the Oracle documentation, explaining the different types of Data Dictionary views available by group and permissions as well as giving examples on how to retrieve data from th…
This video shows how to recover a database from a user managed backup

759 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

19 Experts available now in Live!

Get 1:1 Help Now