Solved

Parsing the XML data to SQL Server

Posted on 2016-09-29
4
50 Views
Last Modified: 2016-09-30
I have dozens of these kind of file from a client that I need to store in the database as separate columns and other important identifier information.

How can I extract the Question: What is the color of Orange and 4 response options for it? I also want to extract all information from the "key" node with values in the ID, EVAL attribute as well.

<TestItem  CLASS="MOAT" METHOD="Multiple">
  <div id="stim" style="width:455px;text-align:center;margin:10px 10px 10px 0px;height:580px;background-color:#ffffff;padding:10px;">
    <div style=" margin:10px; text-align:center;background-color:#ffffff;">
      <table>
        <tr>
          <td valign="middle" align="right">
            <img src="../images/test1/test1.jpg" />
          </td>
          <td valign="middle" align="left">
            <div class="inItemEnclosedbubble">
              What is the color of Orange?
            </div>
          </td>
        </tr>
      </table>
      <br/>
      <br/>
      <div style="text-align:left;margin:auto;">
       
<div style="height:30px"></div>
      <ssmcblock ID="TEST_81330">
        <distractor ID="A">
          <div class="innerDistractor">
            Orange.
          </div>
        </distractor>
        <distractor ID="B">
          <div class="innerDistractor">
            Blue.

          </div>
        </distractor>
        <distractor ID="C">
          <div class="innerDistractor">
            Red.

          </div>
        </distractor>

        <distractor ID="D">
          <div class="innerDistractor">
            Green.
          </div>
        </distractor>



      </ssmcblock>
      </div>
    </div>

    </div>

 
  <Key>
    <part ID="TEST_81330" EVAL="TESTMC">4</part>
  </Key>  
</TestItem>
Test.xml
0
Comment
Question by:asp123
  • 2
4 Comments
 
LVL 17

Expert Comment

by:Pawan Kumar Khowal
ID: 41821888
Here it is...

CREATE TABLE WorkingTable(Data XML)
Go

INSERT INTO WorkingTable
SELECT * FROM OPENROWSET (BULK 'E:\Pawan.xml', SINGLE_BLOB) AS data
GO

DECLARE @XML AS XML, @hDoc AS INT
SELECT @XML = Data FROM WorkingTable

EXEC sp_xml_preparedocument @hDoc OUTPUT, @XML

SELECT Id,text
FROM OPENXML(@hDoc, '/TestItem/div/div')
WHERE LocalName = '#text' AND text LIKE '%Orange%' OR  text LIKE '%Blue%' OR  text LIKE '%Red%' OR  text LIKE '%Green%'

EXEC sp_xml_removedocument @hDoc

---

Open in new window



Enjoy !! Pawan
0
 

Author Comment

by:asp123
ID: 41821963
Is there a way for me to get the text value along with class value?

            <div class="inItemEnclosedbubble">
              What is the color of Orange?
            </div>

          <div class="innerDistractor">
            Blue.
          </div>

I want the data as

inItemEnclosedbubble, What is the color of Orange?
innerDistractor,  Blue.
0
 
LVL 49

Expert Comment

by:Ryan Chong
ID: 41823024
try something like this:
IF OBJECT_ID('tempdb..#WorkingTable') IS NOT NULL DROP TABLE #WorkingTable
Go

CREATE TABLE #WorkingTable(Data XML)
Go

INSERT INTO #WorkingTable
SELECT * FROM OPENROWSET (BULK 'D:\yourFolder\Test.xml', SINGLE_BLOB) AS data
GO

DECLARE @XML AS XML, @hDoc AS INT
SELECT @XML = Data FROM #WorkingTable

EXEC sp_xml_preparedocument @hDoc OUTPUT, @XML

SELECT *
FROM OPENXML(@hDoc, '//distractor/div')
WITH (
	Question		varchar(100) '/TestItem/div/div/table/tr/td[2]/div',
	QuestionClass	varchar(100) '/TestItem/div/div/table/tr/td[2]/div/@class',
	AnswerNo		varchar(10) '../@ID',
    Answer			varchar(100) '..' ,
    AnswerClass		varchar(50) '@class'   
) 

EXEC sp_xml_removedocument @hDoc

Open in new window

0
 
LVL 17

Accepted Solution

by:
Pawan Kumar Khowal earned 500 total points
ID: 41823032
Here it is with output...

IF OBJECT_ID('tempdb..#WorkingTable') IS NOT NULL DROP TABLE #WorkingTable
Go

CREATE TABLE #WorkingTable(Data XML)
Go

INSERT INTO #WorkingTable
SELECT * FROM OPENROWSET (BULK 'E:\Pawan.xml', SINGLE_BLOB) AS data
GO

DECLARE @XML AS XML, @hDoc AS INT
SELECT @XML = Data FROM #WorkingTable

EXEC sp_xml_preparedocument @hDoc OUTPUT, @XML

SELECT DISTINCT *
FROM OPENXML(@hDoc, '//distractor/div')
WITH (
	Question		varchar(100) '/TestItem/div/div/table/tr/td[2]/div',
	QuestionClass	varchar(100) '/TestItem/div/div/table/tr/td[2]/div/@class'	
) 
UNION ALL
SELECT *
FROM OPENXML(@hDoc, '//distractor/div')
WITH 
(
	AnswerNo		varchar(10) '../@ID',    
    AnswerClass		varchar(50) '@class'  
) 

EXEC sp_xml_removedocument @hDoc

--

Open in new window

--


Output

Question                                        QuestionClass
What is the color of Orange?      inItemEnclosedbubble
A                                                      innerDistractor
B                                                      innerDistractor
C                                                      innerDistractor
D                                                      innerDistractor

Enjoy !!
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Browsing the questions asked to the Experts of this forum, you will be amazed to see how many times people are headaching about monster regular expressions (regex) to select that specific part of some HTML or XML file they want to extract. The examp…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

707 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