Austin Efnor
Austin Efnor

Reputation: 166

How do you grab an attribute of child node in XML

I'm looking for a method to grab the an attribute of a child node from the parent node. So far I have:

For each dataNodedsd in xmlDocBindings.SelectNodes("//dataobject")
    nodesDsdID = dataNodedsd.getAttribute("objectid")
    set parentNode = dataNodedsd.parentNode
    if d.Exists(nodesDsdID) Then
        d.item(nodesDsdID) = parentNode.getAttribute("ID") 
        paramstr = parentnode.selectsinglenode("property[@name='pointrefparamname']").text 
        msgbox paramStr
        d2key = parentNode.getAttribute("ID")

        'add ids to dict2

        d2.add d2key, ""
    End If

I am currently looking to pull paramstr from the parent node. My current attempt was using the code provided

paramstr = parentnode.selectsinglenode("property[@name='pointrefparamname']").text

However I am failing to properly pull the string from the node.

This is a sample of XML that I am attempting to pull from the node:

<dataobject format="propertybag" type="HMIPage.Generic" id="3">

<property name="AddressFlags">1</property>

<property name="AddressType">0</property>

<property name="CalloutElement"/>

<property name="ObjectType">0</property>

<property name="ParameterFormat">0</property>

<property name="PointRefFlags">0</property>

<property name="PointRefParamName">PIDA.MODEFL.CAS</property>

<property name="PointRefParamOffset">0</property>

<property name="PointRefPointName">00FC1627</property>

<property name="PresentationType">0</property>

<property name="SecurityLevel">0</property>

<property name="UpdatePeriod">0</property>

<property name="version">1.3</property>

</dataobject>

I am simply trying to pull from

<property name="PointRefParamName">PIDA.MODEFL.CAS</property>

and get the result

PIDA.MODEFL.CAS

as a string. So far everything I have looked up either hasn't been working(either to my incompetence/misunderstanding or lack of viable methodology. If anyone could clear this up it would be greatly appreciated. Once again I am simply attempting to grab an attribute from a child node within a parent node.

Upvotes: 2

Views: 63

Answers (1)

Kevin Brown
Kevin Brown

Reputation: 8857

This works for me in VBA

Sub getValXml()
  Dim myDocument As DOMDocument30
  Dim parNodes As IXMLDOMNodeList
  Dim parNode As IXMLDOMNode
  Dim myNode As IXMLDOMNode
  Set myDocument = New DOMDocument30

  myDocument.Load ("C:\temp\text.xml")
  Set parNodes = myDocument.SelectNodes("//dataobject")
  For Each parNode In parNodes
    Set myNode = parNode.SelectSingleNode("property[@name='PointRefParamName']")
    MsgBox myNode.Text
  Next
End Sub

The output is enter image description here

Upvotes: 1

Related Questions