Solved

Parse XML in SQL

Posted on 2011-03-24
2
392 Views
Last Modified: 2012-05-11
In my (partial) attached code it works...and I can output my table...but it's only picking up the FIRST  <ErrorText> content.
I need to create two identical lines of data with the ErrorText line different

I Execute it with this code
Declare @info nvarchar(300)
Set @info = 'Errors'
Declare @xml xml
Set @xml = '<?xml version="1.0" encoding="UTF-8" ?>
  <Document>
  <XMLVersion version="1.00" />
  <Record>
  <PacketID>0000410403584837143000</PacketID>
  <PacketSequence>1</PacketSequence>
  <Ref1 />
  <Ref2 />
  <Ref3 />
  <Ref4 />
  <LienRefNum />
  <OrderNumber />
  <Status type="Error" />
  <Errors>
  <ErrorText>Invalid XML Document</ErrorText>
  <ErrorText>The element Names has invalid child element IndividualName. List of possible elements expected: MailAddress.</ErrorText>
  </Errors>
  </Record>
  </Document>'

exec sp_portal_UCC_ProcessStatusReplies_BU_LB_0324 @info, @xml
ALTER PROCEDURE [dbo].[sp_portal_UCC_ProcessStatusReplies_BU_LB_0324] (@xmlRequestInfo varchar(300), @xmlData xml)
AS
BEGIN
--============================================================================
--Create Temp Table and insert data
--============================================================================
	CREATE TABLE #XmlImport   
		(    
			xmlFileInfo VARCHAR(300) NOT NULL,    
			xml_data XML NOT NULL    
		) 
		
	Insert into #XmlImport
       (xmlFileInfo,xml_data)
	Select @xmlRequestInfo, @xmlData
--============================================================================
--Process Data for Select
--============================================================================
 -- variable to store the XML content
	declare @xml_text varchar(max)

 -- xml document handle             
	declare @i int                                         
  
-- get XML content from XML data type field in table into a variable  
	select @xml_text = cast(xml_data as varchar(max)) from #XmlImport  
  
-- prepare the XML document  
	exec sp_xml_preparedocument @i OUTPUT, @xml_text  

-- Reply variable
	declare @response nvarchar(200)
	set @response = @xmlRequestInfo + '-Done'

select @response as sqlReply, * from  
openxml(@i,'/Document/Record',2)  
with  
(  		
	 PacketID nvarchar(100)  
	,PacketSequence nvarchar(10)
	,Ref1 nvarchar(40) 'Ref1'
	,Ref2 nvarchar(2000) 'Ref2'
	,Ref3 nvarchar(200) 'Ref3'
	,Ref4 nvarchar(40) 'Ref4'
	,LienRefNum nvarchar(10) 'LienRefNum'
	,OrderNumber nvarchar(40) 'OrderNumber'
	,ErrorText nvarchar(1000) 'Errors/ErrorText'

) s


End

Open in new window

0
Comment
Question by:lrbrister
[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
2 Comments
 
LVL 58

Accepted Solution

by:
cyberkiwi earned 500 total points
ID: 35211630
alter PROCEDURE [dbo].[sp_portal_UCC_ProcessStatusReplies_BU_LB_0324] (@xmlRequestInfo varchar(300), @xmlData xml)
AS
BEGIN
-- Reply variable
	declare @response nvarchar(200)
	set @response = @xmlRequestInfo + '-Done'

select @response as sqlReply,
	q.n.value('PacketID[1]', 'nvarchar(100)') PacketID,
	q.n.value('PacketSequence[1]', 'nvarchar(10)') PacketSequence,
	q.n.value('Ref1[1]', 'nvarchar(40)') Ref1,
	q.n.value('Ref2[1]', 'nvarchar(2000)') Ref2,
	q.n.value('Ref3[1]', 'nvarchar(200)') Ref3,
	q.n.value('Ref4[1]', 'nvarchar(40)') Ref4,
	q.n.value('LienRefNum[1]', 'nvarchar(10)') LienRefNum,
	q.n.value('OrderNumber[1]', 'nvarchar(40)') OrderNumber,
	e.t.value('.[1]', 'nvarchar(1000)') [Errors/ErrorText]
from @xmlData.nodes('/Document/Record') q(n)
outer apply q.n.nodes('Errors/ErrorText') e(t)
End
go

Open in new window

0
 

Author Closing Comment

by:lrbrister
ID: 35214159
Perfect...thanks.
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

Suggested Solutions

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

739 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