Solved

Parsing XML held in a Clob field

Posted on 2008-11-03
7
1,784 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.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
one-way data "masking" MD5 sql 26 145
JDeveloper 12c for 32 bit 4 68
Oracle Database Upgrade 13 60
ORA-00923: FROM keyword not found where expected 3 23
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…
Truncate is a DDL Command where as Delete is a DML Command. Both will delete data from table, but what is the difference between these below statements truncate table <table_name> ?? delete from <table_name> ?? The first command cannot be …
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…
This video explains what a user managed backup is and shows how to take one, providing a couple of simple example scripts.

911 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

20 Experts available now in Live!

Get 1:1 Help Now