mardi 5 mai 2015

XML Syntax in VBA

I am trying to collect all descendants of a given element.

My XML file looks like this:

<?xml version="1.0" encoding="utf-8"?>
<ArrayOfInputs2 xmlns:xsi="http://ift.tt/ra1lAU" xmlns:xsd="http://ift.tt/tphNwY">
  <Inputs2>
    <Tag>Selected-1</Tag>
    <Var1>34</Var1>
    <Results>
      <resultModelView>
        <ModeResults>
          <Var2>69</Var2>
        </ModeResults>
      </resultModelView>
      <resultModelView>
        <ModeResults>
          <Var3>70</Var3>
        </ModeResults>
      </resultModelView>
    </Results>
  </Inputs2>
  <Inputs2>
    <Tag>Selected-2</Tag>
    <Var1>17</Var1>
    <Results>
      <resultModelView>
        <ModeResults>
          <Var2>89</Var2>
        </ModeResults>
      </resultModelView>
      <resultModelView>
        <ModeResults>
          <Var3>45</Var3>
        </ModeResults>
      </resultModelView>
    </Results>
  </Inputs2>
</ArrayOfInputs2>

User selects a given Tag, and I'd like to write all descendants within Inputs2 from that selected Tag to an IXMLDOMElement variable so I can format and display this variable results. My current VBA code to do this is below. When I run this, I only get the Tag and Var1 values, all children beneath that aren't accessible.

Function get_by_tag(ByRef xd As MSXML2.DOMDocument60, ByVal Tag As String)

    Dim seltag As String
    seltag = Chr(34) & Tag & Chr(34)

    Set foo = xd.SelectNodes("//Inputs2[Tag=" & seltag & "]")
    If foo.Length > 0 Then
        Set get_by_tag = foo
    Else
        Set get_by_tag = Nothing
    End If
    
End Function

Any assistance would be greatly appreciated!

Aucun commentaire:

Enregistrer un commentaire