Solved

Parsing XML held in a Clob field

Posted on 2008-11-03
7
1,788 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
Technology Partners: 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

Independent Software Vendors: 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

Configuring and using Oracle Database Gateway for ODBC Introduction First, a brief summary of what a Database Gateway is.  A Gateway is a set of driver agents and configurations that allow an Oracle database to communicate with other platforms…
Shell script to create broker configuration file using current broker Configuration, solely for purpose of backup on Linux. Script may need to be modified depending on OS-installation. Please deploy and verify the script in a test environment.
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Via a live example, show how to take different types of Oracle backups using RMAN.

749 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