Solved

Parsing XML into SQL

Posted on 2014-07-23
2
374 Views
Last Modified: 2014-07-23
Hello,

I have the following XML file which has data in the attributes and within the tags I trying to figure out the best approach to get this data into SQL. I am able to pull out the attributes but what happens when I pull out the data with in the tags it is all concatenated together as one value.

What would be the best approach to this multiple passes?

LJ

<Package>
 <InvestmentVehicle _Id="F00000Q9RW">
        <Name LanguageId="0L00000122">Need Data From Here</Name>
        <LegalName LanguageId="0L00000122">Need Data From Here</LegalName>
      <FundServList>
          <FundServId CurrencyId="CU$$$$$USD">PMO2406</FundServId>
        </FundServList>
</InvestmentVehicle>
<InvestmentVehicle _Id="F00000LXW6">
        <Name LanguageId="0L00000138">CAN Revenu (M) 100/100</Name>
        <Name LanguageId="0L00000122">CAN Income (M) 100/100</Name>
        <LegalName LanguageId="0L00000138">CAN Revenu (M) 100/100</LegalName>
        <LegalName LanguageId="0L00000122">CAN Income (M) 100/100</LegalName>
      <FundServList>
        <FundServId CurrencyId="CU$$$$$CAD">CAN1073</FundServId>
        <FundServId CurrencyId="CU$$$$$CAD">CAN873</FundServId>
        <FundServId CurrencyId="CU$$$$$CAD">CAN973</FundServId>
        </FundServList>
 </InvestmentVehicle>
</Package>
0
Comment
Question by:Jordan_WM
[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 33

Accepted Solution

by:
ste5an earned 435 total points
ID: 40215055
Well, it would have been nice, if you would include your solutions so far.. E.g.
DECLARE @Xml XML; 
SET @Xml = N'
<Package>
	<InvestmentVehicle _Id="F00000Q9RW">
		<Name LanguageId="0L00000122">Need Data From Here</Name>
		<LegalName LanguageId="0L00000122">Need Data From Here</LegalName>
		<FundServList>
			<FundServId CurrencyId="CU$$$$$USD">PMO2406</FundServId>
		</FundServList>
	</InvestmentVehicle>
	<InvestmentVehicle _Id="F00000LXW6">
		<Name LanguageId="0L00000138">CAN Revenu (M) 100/100</Name>
		<Name LanguageId="0L00000122">CAN Income (M) 100/100</Name>
		<LegalName LanguageId="0L00000138">CAN Revenu (M) 100/100</LegalName>
		<LegalName LanguageId="0L00000122">CAN Income (M) 100/100</LegalName>
		<FundServList>
			<FundServId CurrencyId="CU$$$$$CAD">CAN1073</FundServId>
			<FundServId CurrencyId="CU$$$$$CAD">CAN873</FundServId>
			<FundServId CurrencyId="CU$$$$$CAD">CAN973</FundServId>
		</FundServList>
	</InvestmentVehicle>
</Package>
';

SELECT	InvestmentVehicle.value('@_Id', 'NVARCHAR(255)') AS ID ,
		Name.value('@LanguageId', 'NVARCHAR(255)') AS NameLanguageID ,
		Name.value('.', 'NVARCHAR(255)') AS NameText ,
		LegalName.value('@LanguageId', 'NVARCHAR(255)') AS LegalNameLanguageID ,
		LegalName.value('.', 'NVARCHAR(255)') AS LegalNameText ,
		FundServId.value('@CurrencyId', 'NVARCHAR(255)') AS FundServIdCurrencyId ,
		FundServId.value('.', 'NVARCHAR(255)') AS FundServIdText
FROM	@Xml.nodes('/Package/InvestmentVehicle') A ( InvestmentVehicle )
	CROSS APPLY InvestmentVehicle.nodes('Name') B ( Name )
	CROSS APPLY InvestmentVehicle.nodes('LegalName') C ( LegalName ) 
	CROSS APPLY InvestmentVehicle.nodes('FundServList/FundServId') D ( FundServId ) ;

Open in new window

0
 

Author Closing Comment

by:Jordan_WM
ID: 40215225
Thank you wasn't looking for a full solution, I was getting frustrated in trying to go the data out. I ended up getting the right number of records but the columns were not updating with new values


DECLARE @XML AS XML, @hDoc AS INT, @SQL NVARCHAR (MAX)

SELECT @XML = XmlCol FROM T

EXEC sp_xml_preparedocument @hDoc OUTPUT, @XML

SELECT _Id--, Name--, LegalName, CurrencyId
, FundServId
FROM OPENXML(@hDoc, 'Package/InvestmentVehicle/FundServList/FundServId')
WITH
(
_Id [varchar](50) '../../@_Id',
Name [varchar](100) '../../Name',
LegalName [varchar](100) '../../LegalName',
CurrencyId [varchar](100) '@CurrencyId',
FundServId [varchar](100) '../FundServId'

)

EXEC sp_xml_removedocument @hDoc
GO
0

Featured Post

Technology Partners: 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

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…
SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

726 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