I am new in XML. But I have done coding in VBA, C, and HTML. I am using VBA to extract data from an xml file. I use Xpathbuilder to generate Xpath queries for my work. However, some Xpath queries are causing errors in my program. As I am new in xml and its standards I am not sure what is causing them. Answers to the following questions might help me in understanding these problems better. Can you please help me? Thanks.
1. Which version of xml am I using? In my program I declare variables as shown here. Dim oDoc As MSXML2.DOMDocument, Dim oNodeList As msxml2.IXMLDOMNodeList. Also, in Refrences of the VBA environment I have selected Microsoft xml.V6.0. I have Microsoft XP and Office 2003 SP2.
2. Why the SelectionLanguage property is not working? When I put the following statement in my code, the selectnodes statement gives me an error. Selectnodes work fine without this statement. I don’t know why this is happening.
oDoc.setProperty "SelectionLanguage", "XPath"
3. Why the Selectnodes statement is not working for a few queries? I am using this statement:
Set oChildren = oDoc.selectNodes(Path). This statement works for simple queries such as
Path = /FSK:Research/FSK:Product/FSK:Context/FSK:IssuerDetails/FSK:Issuer/FSK:SecurityDetails/FSK:Security
But causes an error for
Path =
/FSK:Research/FSK:Product/FSK:Context/FSK:IssuerDetails/FSK:Issuer/FSK:SecurityDetails/FSK:Security [@securityID='1404']
Or Path =
/FSK:Research/FSK:Product/FSK:Context/FSK:IssuerDetails/FSK:Issuer/FSK:SecurityDetails/FSK:Security/FSK:FinancialGroup [2]
Note that I have verified these paths using Xpathbuilder.
I am also attaching my code below.
Thanks.
Expand|Select|Wrap|Line Numbers
- Dim oDoc As msxml2.DOMDocument
- Dim fSuccess As Boolean
- Dim oRoot As msxml2.IXMLDOMNode
- Dim oBegining As msxml2.IXMLDOMNode
- Dim oAttributes As msxml2.IXMLDOMNamedNodeMap
- Dim oCountryName As msxml2.IXMLDOMNode
- Dim oChildren As msxml2.IXMLDOMNodeList
- Dim oChild As msxml2.IXMLDOMNode
- Dim oNodeList As msxml2.IXMLDOMNodeList
- Dim Startcell As Range
- Dim temp As String
- Dim temp2 As String
- Dim Colno As Integer
- Dim oCell As Range
- Dim Path As String
- Dim Counter As Integer
- On Error GoTo Exitfun
- Set oDoc = CreateObject("MSXML2.DOMDocument")
- oDoc.async = False
- oDoc.validateOnParse = False
- oDoc.Load ("c:\test.xml")
- ' MsgBox oDoc.XML
- Worksheets("Template").Select
- Set oRoot = oDoc.documentElement
- Set oNodeList = oDoc.getElementsByTagName("FSK:SecurityName") This works fine.
- temp = oNodeList.Item(0).childNodes.Item(0).Text
- Set Startcell = ActiveCell
- Counter = 0 ' to count columns
- For Each oCell In Worksheets("Instructions - Introduction").Range("G1:G21")
- Path = oCell.Value
- Set oChildren = oDoc.selectNodes(Path) This doesn't work sometimes.
- Startcell.Offset(0, Counter).Value = oChildren.Length
- Counter = Counter + 1
- Next
- Exit Sub
- Exitfun: MsgBox "Error occured" & Counter
- End Sub
- --------------------------------------------------------------------------------
- Last edited by musicgold