替换VB6中的XML节点中的值
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了替换VB6中的XML节点中的值相关的知识,希望对你有一定的参考价值。
我的目标是使用MSXML2.DOMDocument在VB6中的XML节点中查找和替换(id,name,status)的值。我正在使用较旧的代码,但我并不认为这很困难我可以使用getElementsByTagName从xml中提取我需要的确切节点。我目前为每个值都有setAttributes,但它没有更改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
我的预期结果是
<offerstatus id="1" name="ACTIVE" inactive="FALSE">
但是,id,name和inactive在XML中保持相同的值。一般结构是这样的
<groups>
<group>
<offerstatus/>
</group>
</groups>
如果这有助于任何。然后我可以访问节点offerstatus
。
答案
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
以上是关于替换VB6中的XML节点中的值的主要内容,如果未能解决你的问题,请参考以下文章