Coding MSXML going straight to the node of interest rather than snooping and looping

mdlueck

Sr. Application Developer
Local time
Yesterday, 22:57
Joined
Jun 23, 2011
Messages
2,650
I have successfully used MSXML v6 objects to traverse some application specific XML. That code uses what I call "snooping and looping" to navigate the XML tree.

Now I am ready to start building XML reading into my application. I would like to open to specific known nodes without needing to arrive at them eventually via "snooping and looping".

Following is a sample of my XML:

Code:
<FASMessage>
  <XMLSchemaVersion>2.1.0.1</XMLSchemaVersion>
  <FASRule>fasruletest1</FASRule>
  <FASRuleMessage>
    <Field1>Test 1</Field1>
    <Field2>Test 2</Field2>
    <Field3>Test 3</Field3>
    <Field4>Test 4</Field4>
  </FASRuleMessage>
</FASMessage>
I would like to first validate the XMLSchemaVersion field. Next verify that the FASRule is what it is suppose to be for the particular operation being executed (The FASRule in the XML must match the DB field of the row which the XML was selected from.), then finally dig into the custom field list contained in FASMessage.

I have come across such sample code that is not very encouraging about knowing where you want to go within XML and to go there directly:

"VBA code to iterate through the results of GetListCollection web service from Sharepoint 2007"
http://the-simple-programmer.blogsp...ough-results-of.html?_sm_au_=iVVfsFt4t0tZJR6F

Suggestions of code not utilizing the "snooping and looping" navigation method?
 
Last edited:
It appears the MSXML object is able to traverse the XML several layers deep in one call to the object. Thus my work-around to avoid "snooping and loop" code.

Code:
  'Open the XMLSchemaVersion
  Set objXMLNodeXMLSchemaVersion = objXMLDOMDoc.selectSingleNode("//FASMessage/XMLSchemaVersion")
  If objXMLNodeXMLSchemaVersion Is Nothing Then
    GoTo Exit_LoadXML
  End If

  'Save the XMLSchemaVersion
  Me.XMLSchemaVersion = objXMLNodeXMLSchemaVersion.Text

  'Open the FASRule
  Set objXMLNodeFASRule = objXMLDOMDoc.selectSingleNode("//FASMessage/FASRule")
  If objXMLNodeFASRule Is Nothing Then
    GoTo Exit_LoadXML
  End If

  'Save the FASRule
  Me.FASRule = objXMLNodeFASRule.Text
And I went fancy and saved a portion of the XML right in my custom class object as the MSXML object containing only that portion of the entire XML:
Code:
  'Open the FASRuleMessage
  Set objXMLNodeFASRuleMessage = objXMLDOMDoc.selectSingleNode("//FASMessage/FASRuleMessage")
  If objXMLNodeFASRuleMessage Is Nothing Then
    GoTo Exit_LoadXML
  End If

  'Save the FASRuleMessage as XML
  Me.FASRuleMessage = objXMLNodeFASRuleMessage
Pretty slick, time for lunch! :cool:
 
Last edited:

Users who are viewing this thread

Back
Top Bottom