Solved

Parsing the XML data to SQL Server

Posted on 2016-09-29
4
70 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 28

Expert Comment

by:Pawan Kumar
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 51

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 28

Accepted Solution

by:
Pawan Kumar 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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
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.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

696 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