0

My goal is to find and replace the values for (id, name, status) within an XML Node in VB6 using MSXML2.DOMDocument. I am working with older code but I didnt expect it to be this difficult I can pull the exact node I need from within the xml with getElementsByTagName. I currently have setAttributes for each value but it is not changing the node within the XML.

Dim oDOMOffer As New MSXML2.DOMDocument
Dim oldPendingNode As IXMLDOMElement
Dim newActiveNode As IXMLDOMElement
Dim sXMLGroup As String
Dim strId As String
Dim strName As String
Dim strInactive As String

On Error GoTo ErrHandler

'---
TraceEntry m_sProgID, csFunction
TraceParams m_sProgID, csFunction, Array("sXMLOffer", "sXMLOrigOffer"), Array(sXMLOffer, sXMLOrigOffer)
'---



'---
TraceEntry m_sProgID, csFunction
TraceParams m_sProgID, csFunction, Array("sXMLOffer", "sXMLGroup"), Array(sXMLOffer, sXMLGroup)
'---

'Take what we can get...
sXMLGroup = sXMLOffer

' Create a new element.
strId = "1"
strName = "ACTIVE"
strInactive = "FALSE"
''''''''newActiveNode = "<" & "offerstatus id='1' name='ACTIVE' inactive='FALSE'" & ">"


If oDOMOffer.loadXML(sXMLOffer) = True Then
    If Not oDOMOffer.getElementsByTagName("group").nextNode Is Nothing Then
    sXMLGroup = oDOMOffer.getElementsByTagName("group").nextNode.XML

        'sXMLGroup = oDOMOffer.getElementsByTagName("offerstatus").nextNode.XML
        'oldPendingNode = sXMLGroup
        'oDOMOffer = oDOMOffer.replaceChild(newActiveNode, oldPendingNode).XML

        ' For starting User Name.
        Set newActiveNode = oDOMOffer.createElement("offerstatus")

         ' Create an attribute and set its value to that of the new.
        newActiveNode.setAttribute "id", strId
        newActiveNode.setAttribute "name", strName
        newActiveNode.setAttribute "inactive", strInactive

        oDOMOffer.documentElement.appendChild newActiveNode

        '---
        Trace m_sProgID, csFunction, "sXMLOfferstatus:" & sXMLGroup
        '---

        '---
        'Trace m_sProgID, csFunction, "newActiveNode:" & newActiveNode
        '---
        sXMLGroup = oDOMOffer.getElementsByTagName("group").nextNode.XML


        '---
        Trace m_sProgID, csFunction, "NewsXMLGroup:" & sXMLGroup
        '---
    Else
        '---
        Trace m_sProgID, csFunction, "ERROR: <group> node not found in sXMLOffer document text"
        '---
    End If
End If

My expected results would be

<offerstatus id="1" name="ACTIVE" inactive="FALSE">

but instead the id, name and inactive is keeping the same values in the XML. The general structure is this

<groups>
  <group>
    <offerstatus/>
  </group>
</groups>

if this helps any. Then I am able to access the node offerstatus.

StayOnTarget
  • 11,743
  • 10
  • 52
  • 81
  • I looked at using the ReplaceChild but it also didnt work, but it very well may have been the way I had the code set up. – Samantha Iren Martin Apr 30 '19 at 17:00
  • Looks to me that after you append the new child element to the document, you call your trace routine and output the original xml data structure. If the posted code reflects what you're actually running, then you want to trace out the updated DOM xml data? – MarkL Apr 30 '19 at 17:04
  • When I go to trace oDOMOffer it throws an error. – Samantha Iren Martin Apr 30 '19 at 17:15
  • 1
    "it throws an error". Not helpful. What did you try? What was the error? Calling the `oDOMOffer.xml` method should provide a string with the document's xml representation. – MarkL Apr 30 '19 at 18:52
  • You are correct I didnt have .XML to convert to string. Sorry to be so vague. I think I have figured it out, I was close just needed to set nodes and elements to the DOM – Samantha Iren Martin Apr 30 '19 at 18:55
  • 2
    Also - you're adding the new element to the document root (``), not to a `` sub-tree. – MarkL Apr 30 '19 at 18:57

1 Answers1

1
Dim oDOMOffer As New MSXML2.DOMDocument
Dim newActiveNode As IXMLDOMElement
Dim objNode As IXMLDOMNode
Dim objOffer As IXMLDOMElement
Dim sXMLGroup As String

On Error GoTo ErrHandler

'---
TraceEntry m_sProgID, csFunction
TraceParams m_sProgID, csFunction, Array("sXMLOffer", "sXMLOrigOffer"), Array(sXMLOffer, sXMLOrigOffer)
'---

'Take what we can get...
sXMLGroup = sXMLOffer

TraceParams m_sProgID, csFunction, Array("sXMLGroup"), Array(sXMLGroup)


If oDOMOffer.loadXML(sXMLOffer) = True Then
    If Not oDOMOffer.getElementsByTagName("group").nextNode Is Nothing Then
    sXMLGroup = oDOMOffer.getElementsByTagName("group").nextNode.XML

        Set objNode = oDOMOffer.getElementsByTagName("offerstatus").nextNode
        Set newActiveNode = objNode

        newActiveNode.setAttribute "id", "1"
        newActiveNode.setAttribute "name", "ACTIVE"
        newActiveNode.setAttribute "inactive", "FALSE"

        '---
        sXMLGroup = oDOMOffer.getElementsByTagName("offerstatus").nextNode.XML

        '---
        Trace m_sProgID, csFunction, "NewsOfferStatus:" & sXMLGroup
        '---
    Else
        '---
        Trace m_sProgID, csFunction, "ERROR: <group> node not found in sXMLOffer document text"
        '---
    End If
End If