Solved

Parsing XML held in a Clob field

Posted on 2008-11-03
7
1,789 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 74

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 74

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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 74

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 74

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Note: this article covers simple compression. Oracle introduced in version 11g release 2 a new feature called Advanced Compression which is not covered here. General principle of Oracle compression Oracle compression is a way of reducing the d…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
This video explains at a high level about the four available data types in Oracle and how dates can be manipulated by the user to get data into and out of the database.
Via a live example, show how to restore a database from backup after a simulated disk failure using RMAN.

688 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