Hi
I have managed to achieve the objective which was to import records from xml file that would match my parameter (e.g. price >100)into excel spreadsheet, however i have a sneaky suspission that there is an easier (and faster) way of doing this than using loops.
Ideally i would like to replace the loop part of my code with something like sql statement
"Select... from... where price>100" which would give me a recordset which then i could write to excel in one go.
i have found some info on XQuery but not including explanation on how to use it within VBA - can anyone help?
How do you connect connect to xml file, nest xml parameters in sql select statement?
Could you please drop me an example line of code or point me to a good tutorial on the net?
i am pasting below my code and xml file :
----------------------------------------------------------
<?xml version="1.0" ?>
- <GiltValuation>
<GiltInfo SecID="3328033" SecName="TREASURY STOCK 4 3/4" SecPrice="101.0228" />
<GiltInfo SecID="3383485" SecName="ALLIED IRISH BANKS 2003 5.625" SecPrice="82.0995" />
<GiltInfo SecID="3398199" SecName="ANGEL TRAINS FIN. 2003 5 1/4" SecPrice="99.5711" />
<GiltInfo SecID="7133880" SecName="PRINCIPAL FINL.GLB. 2001 6%" SecPrice="101.2217" />
<GiltInfo SecID="B00NY17" SecName="TREASURY STOCK 4 3/4" SecPrice="103.2779" />
<GiltValuation>
-----------------------------------------------------
Sub ImportXMLData()
Dim oXMLDOC As DOMDocument 'this represents the whole XMl document
Dim oXMLNode As IXMLDOMNode
Dim oXMLNodeList As IXMLDOMNodeList 'list matching nodes of XMl document
Dim sXPath As String 'XPath syntax
Dim sXMLFilePath As String ' Path to xml file is taken from excel cell
sXMLFilePath = Range("xml_path").Value
Set oXMLDOC = New DOMDocument
'Load XML file selected by user (take path from excel cell)
With oXMLDOC
.async = False
.validateOnParse = False
.Load (sXMLFilePath)
End With
'Find all nodes where security price attribute is higher than 100
sXPath = "//GiltValuation/GiltInfo[@SecPrice>100]"
Set oXMLNodeList = oXMLDOC.selectNodes(sXPath)
'In case of no-match:
If oXMLNodeList Is Nothing Then
MsgBox "XMl data not found"
Else
'test - show results in msgbox (show security name and price - which should be >100)
For Each oXMLNode In oXMLNodeList
result = result & vbNewLine & Left(oXMLNode.Attributes.getNamedItem("SecName").nodeValue, 10) & "---" & oXMLNode.Attributes.getNamedItem("SecPrice").nodeValue
Next
End If
MsgBox result
End Sub
I have managed to achieve the objective which was to import records from xml file that would match my parameter (e.g. price >100)into excel spreadsheet, however i have a sneaky suspission that there is an easier (and faster) way of doing this than using loops.
Ideally i would like to replace the loop part of my code with something like sql statement
"Select... from... where price>100" which would give me a recordset which then i could write to excel in one go.
i have found some info on XQuery but not including explanation on how to use it within VBA - can anyone help?
How do you connect connect to xml file, nest xml parameters in sql select statement?
Could you please drop me an example line of code or point me to a good tutorial on the net?
i am pasting below my code and xml file :
----------------------------------------------------------
<?xml version="1.0" ?>
- <GiltValuation>
<GiltInfo SecID="3328033" SecName="TREASURY STOCK 4 3/4" SecPrice="101.0228" />
<GiltInfo SecID="3383485" SecName="ALLIED IRISH BANKS 2003 5.625" SecPrice="82.0995" />
<GiltInfo SecID="3398199" SecName="ANGEL TRAINS FIN. 2003 5 1/4" SecPrice="99.5711" />
<GiltInfo SecID="7133880" SecName="PRINCIPAL FINL.GLB. 2001 6%" SecPrice="101.2217" />
<GiltInfo SecID="B00NY17" SecName="TREASURY STOCK 4 3/4" SecPrice="103.2779" />
<GiltValuation>
-----------------------------------------------------
Sub ImportXMLData()
Dim oXMLDOC As DOMDocument 'this represents the whole XMl document
Dim oXMLNode As IXMLDOMNode
Dim oXMLNodeList As IXMLDOMNodeList 'list matching nodes of XMl document
Dim sXPath As String 'XPath syntax
Dim sXMLFilePath As String ' Path to xml file is taken from excel cell
sXMLFilePath = Range("xml_path").Value
Set oXMLDOC = New DOMDocument
'Load XML file selected by user (take path from excel cell)
With oXMLDOC
.async = False
.validateOnParse = False
.Load (sXMLFilePath)
End With
'Find all nodes where security price attribute is higher than 100
sXPath = "//GiltValuation/GiltInfo[@SecPrice>100]"
Set oXMLNodeList = oXMLDOC.selectNodes(sXPath)
'In case of no-match:
If oXMLNodeList Is Nothing Then
MsgBox "XMl data not found"
Else
'test - show results in msgbox (show security name and price - which should be >100)
For Each oXMLNode In oXMLNodeList
result = result & vbNewLine & Left(oXMLNode.Attributes.getNamedItem("SecName").nodeValue, 10) & "---" & oXMLNode.Attributes.getNamedItem("SecPrice").nodeValue
Next
End If
MsgBox result
End Sub