Extract XML data from MS SQL table and import to Excel

MattW1976

New Member
Joined
Apr 13, 2015
Messages
4
I have unformatted XML data in one column of a MS SQL server database. I want to extract it and reformat it so I can import it into excel. What is the best way to do this? Right now, I'm trying to extract it to a file and then import it but it isn't working. I have to extract it to a file, open it in notepad++ and using the XML tools add-in to format it using Pretty print (XML - only with linebreaks) and then it imports just fine. I'd like to be able to just pass it through a function that reformats the XML that way and import it without using a file at all. Here is the code I have so far:


Sub DataExtractToFile()


Dim fFile As Long
Dim strFile As String


strFile = Environ("temp") & "\ugly.xml"
If FileThere(strFile) Then Kill strFile
strFile = Environ("temp") & "\ugly.xml"


fFile = FreeFile


' Create a connection object.
Dim cnITrade As ADODB.Connection
Set cnITrade = New ADODB.Connection


' Provide the connection string.
Dim strConn As String


'Use the SQL Server OLE DB Provider.
strConn = "PROVIDER=SQLOLEDB;"


'Connect to the InternationalTrade database on the local server.
strConn = strConn & "DATA SOURCE=.\SQLExpress;INITIAL CATALOG=InternationalTrade;"


'Use an integrated login.
strConn = strConn & " INTEGRATED SECURITY=sspi;"


'Now open the connection.
cnITrade.Open strConn


' Create a recordset object.
Dim rsITrade As ADODB.Recordset
Set rsITrade = New ADODB.Recordset


With rsITrade
' Assign the Connection object.
.ActiveConnection = cnITrade
' Extract the required records.
.Open "SELECT xml_msg FROM TABLE with (nolock) where xml_msg like '%ABC01156%'"
Open strFile For Output As #fFile
Print #fFile, .GetString(adClipString, 1, "><", vbCrLf, "")
' Tidy up
Close #fFile
.Close
End With


cnITrade.Close
Set rsITrade = Nothing
Set cnITrade = Nothing


End Sub


Function FileThere(FileName As String) As Boolean
FileThere = (Dir(FileName) > "")
End Function




Sub ImportXML(sPath As String)


ActiveWorkbook.XmlImportXml sPath, ImportMap:= _
Nothing, Overwrite:=True, Destination:=Range("$A$2")
End Sub






Here is an example of the unformatted and Pretty printed XML


<Proponix><Header><DestinationID>ABC</DestinationID><SenderID>PRO</SenderID><ClientBank>123</ClientBank><OperationOrganizationID>ABC1</OperationOrganizationID><MessageType>MSG</MessageType><DateSent>20100104</DateSent><TimeSent>161601</TimeSent><MessageID>0000007522</MessageID></Header><SubHeader><InstrumentID>ABC123456</InstrumentID><InstrumentStatus>ABC</InstrumentStatus><ActivityType>CUS</ActivityType><ActivityStatus>REL</ActivityStatus><BusinessDate>20100104</BusinessDate><OriginalActivityType>CUS</OriginalActivityType><RelatedInstrumentID>AB1241550001</RelatedInstrumentID><RelatedActivitySequenceNo>1</RelatedActivitySequenceNo><ProductType>OUT</ProductType><Product>TAC</Product><ProductCategory>US</ProductCategory><RelationshipCustomerID>DESI0101156</RelationshipCustomerID><LimitCustomerID>DESI0101156</LimitCustomerID><BaseCurrency>USD</BaseCurrency><InstrumentCurrency>USD</InstrumentCurrency><LimitCurrency>USD</LimitCurrency><MessageSequenceNumber>000000000020106</MessageSequenceNumber></SubHeader><Body><Activity><DateActivity>20091230</DateActivity><ActivitySequenceNo>0</ActivitySequenceNo><SequenceDate>20100104</SequenceDate><SequenceTime>161559</SequenceTime><ActivityAmount>120506.00</ActivityAmount></Activity><ResultingTerms><InstrumentTerms><DateStart>20091230</DateStart><DateEnd>20100318</DateEnd><ReissuanceInd>N</ReissuanceInd><ConvertedInd>N</ConvertedInd><CountryRisk>US</CountryRisk><TenorCategory>T</TenorCategory></InstrumentTerms><UsanceTerms><DispositionType>HELD</DispositionType><UnconfirmedInd>Y</UnconfirmedInd><NumberOfDays>78</NumberOfDays><TenorDetailType>AFM</TenorDetailType></UsanceTerms></ResultingTerms><Parties><Party><PartyType>DWR</PartyType><CustomerID>ABCI0101156</CustomerID><ReferenceNo>04-COMPUTER-09</ReferenceNo></Body></Proponix>






<Proponix>
<Header>
<DestinationID>ABC</DestinationID>
<SenderID>PRO</SenderID>
<ClientBank>123</ClientBank>
<OperationOrganizationID>ABC1</OperationOrganizationID>
<MessageType>MSG</MessageType>
<DateSent>20100104</DateSent>
<TimeSent>161601</TimeSent>
<MessageID>0000007522</MessageID>
</Header>
<SubHeader>
<InstrumentID>ABC123456</InstrumentID>
<InstrumentStatus>ABC</InstrumentStatus>
<ActivityType>CUS</ActivityType>
<ActivityStatus>REL</ActivityStatus>
<BusinessDate>20100104</BusinessDate>
<OriginalActivityType>CUS</OriginalActivityType>
<RelatedInstrumentID>AB1241550001</RelatedInstrumentID>
<RelatedActivitySequenceNo>1</RelatedActivitySequenceNo>
<ProductType>OUT</ProductType>
<Product>TAC</Product>
<ProductCategory>US</ProductCategory>
<RelationshipCustomerID>DESI0101156</RelationshipCustomerID>
<LimitCustomerID>DESI0101156</LimitCustomerID>
<BaseCurrency>USD</BaseCurrency>
<InstrumentCurrency>USD</InstrumentCurrency>
<LimitCurrency>USD</LimitCurrency>
<MessageSequenceNumber>000000000020106</MessageSequenceNumber>
</SubHeader>
<Body>
<Activity>
<DateActivity>20091230</DateActivity>
<ActivitySequenceNo>0</ActivitySequenceNo>
<SequenceDate>20100104</SequenceDate>
<SequenceTime>161559</SequenceTime>
<ActivityAmount>120506.00</ActivityAmount>
</Activity>
<ResultingTerms>
<InstrumentTerms>
<DateStart>20091230</DateStart>
<DateEnd>20100318</DateEnd>
<ReissuanceInd>N</ReissuanceInd>
<ConvertedInd>N</ConvertedInd>
<CountryRisk>US</CountryRisk>
<TenorCategory>T</TenorCategory>
</InstrumentTerms>
<UsanceTerms>
<DispositionType>HELD</DispositionType>
<UnconfirmedInd>Y</UnconfirmedInd>
<NumberOfDays>78</NumberOfDays>
<TenorDetailType>AFM</TenorDetailType>
</UsanceTerms>
</ResultingTerms>
<Parties>
<Party>
<PartyType>DWR</PartyType>
<CustomerID>ABCI0101156</CustomerID>
<ReferenceNo>04-COMPUTER-09</ReferenceNo>
</Party>
</Body>
</Proponix>
 
Last edited:

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.

Forum statistics

Threads
1,214,591
Messages
6,120,431
Members
448,961
Latest member
nzskater

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top