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
Solved

Parsing XML held in a Clob field

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

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

Subquery in Oracle: Sub queries are one of advance queries in oracle. Types of advance queries: •      Sub Queries •      Hierarchical Queries •      Set Operators Sub queries are know as the query called from another query or another subquery. It can …
Introduction A previously published article on Experts Exchange ("Joins in Oracle", http://www.experts-exchange.com/Database/Oracle/A_8249-Joins-in-Oracle.html) makes a statement about "Oracle proprietary" joins and mixes the join syntax with gen…
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 copy an entire tablespace from one database to another database using Transportable Tablespace functionality.

808 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