VB6 DOMDocument 查找特定注释并删除
VB6 DOMDocument Find a Specific Comment and Delete
Win8.1 上的 VB6
微软 XML v3.0
我徒劳地搜索了 - 我可以使用 VB6 搜索 xml 文件中的元素,但我不知道如何在 xml 文件中搜索特定注释并将其删除。
我发现了一个很棒的 VB6 XML tutorial - 下面的一些示例代码将节点加载到 treeview/WebBrowser 控件中,作为我正在使用的代码类型的示例:
Private Sub cmdPopulate_Click()
Dim objPeopleRoot As IXMLDOMElement
Dim objPersonElement As IXMLDOMElement
Dim tvwRoot As Node
Dim X As IXMLDOMNodeList
Set m_objDOMPeople = New DOMDocument
'this can stop the m_objDOMPeople object from looking
'for external files which in our case are the people.dtd
'and the people.xsl files - set this to true if you want
'the parser to look for the external files
m_objDOMPeople.resolveExternals = True
'this can stop the m_m_objDOMPeople from validating the XML file
'against the people.dtd file - set this to true if you want validation to
'occur
m_objDOMPeople.validateOnParse = True
'load the XML into the dom document, using a string containing
'the XML location
m_objDOMPeople.async = False
Call m_objDOMPeople.Load(m_strXmlPath)
'check that the load of the XML document was successful
If m_objDOMPeople.parseError.reason <> "" Then
' there has been an error with the loaded XML - show the reason
MsgBox m_objDOMPeople.parseError.reason
Exit Sub
End If
'get the root element of the XML - bypassing the comments, PI's etc
Set objPeopleRoot = m_objDOMPeople.documentElement
'Now lets populate the treecontrol from the DOMDocument
'Set Treeview control properties.
tvwPeople.LineStyle = tvwRootLines
tvwPeople.Style = tvwTreelinesPlusMinusText
tvwPeople.Indentation = 400
'check if the treeview has already been populated - if so
'remove the root, which removes everything.
If tvwPeople.Nodes.Count > 0 Then
tvwPeople.Nodes.Remove 1
End If
' add a child to the root node of the TreeView
Set tvwRoot = tvwPeople.Nodes.Add()
tvwRoot.Text = objPeopleRoot.baseName
'iterate through each element in the dom to fill the tree,
'which in itself iterates through each childNode of that
'element(objPersonElement) to drill down into its childNodes
For Each objPersonElement In objPeopleRoot.childNodes
populateTreeWithChildren objPersonElement
Next
webTarget.Navigate m_strXmlPath
cmdDelete.Enabled = True
cmdClear.Enabled = True
End Sub
我在 w3schools 上找到了 COMMENT_NODE - 8,但我不确定如何在 VB6 中使用它
提前致谢
编辑:使用 Bob 的代码
我是这样称呼它的——这对吗?它遍历并似乎找到了每个 .nodetype BUT 评论。我很确定这是操作员错误 - 如果我错误地调用 DeleteTargetComments - 传递了错误的对象 - 正确的方法是什么?
Private m_objDOMPeople As DOMDocument
Private Sub cmdRemoveComments_Click()
' DeleteTargetComments DOM.documentElement
Dim objPeopleRoot As IXMLDOMElement
Set objPeopleRoot = m_objDOMPeople.documentElement
DeleteTargetComments objPeopleRoot
End Sub
MSXML 中的许多功能确实可以帮助优化在慢速脚本语言中的使用。 XPath 就是其中之一,通常除了复杂性外,它给您带来的好处很少。它在非常长的文档上可能具有的任何性能优势都被完全避免使用 DOM 并简单地转向 SAX 解析所取代。
因此,如果您要处理相当小的 XML 文档,VB6 程序可以非常简单地完成此操作。
原文XML:
<doc>
<item>a</item><!-- target to delete -->
<!-- to keep -->
<item>b<!-- target to delete too --></item>
</doc>
您可以只使用:
Private Sub DeleteTargetComments(ByRef Parent As MSXML2.IXMLDOMNode)
Dim Children As MSXML2.IXMLDOMNodeList
Dim Node As MSXML2.IXMLDOMNode
Set Children = Parent.childNodes
'We must check .length because in MSXML the collection iterators are broken:
If Children.length > 0 Then
For Each Node In Children
With Node
If .nodeType = NODE_COMMENT Then
If InStr(LCase$(.Text), "target") Then
Parent.removeChild Node
End If
Else
DeleteTargetComments Node
End If
End With
Next
End If
End Sub
并将其调用为:
DeleteTargetComments DOM.documentElement
结果:
<doc>
<item>a</item><!-- to keep -->
<item>b</item>
</doc>
非常简单,尽管完整的空白保留是另一个主题。
Win8.1 上的 VB6 微软 XML v3.0
我徒劳地搜索了 - 我可以使用 VB6 搜索 xml 文件中的元素,但我不知道如何在 xml 文件中搜索特定注释并将其删除。
我发现了一个很棒的 VB6 XML tutorial - 下面的一些示例代码将节点加载到 treeview/WebBrowser 控件中,作为我正在使用的代码类型的示例:
Private Sub cmdPopulate_Click()
Dim objPeopleRoot As IXMLDOMElement
Dim objPersonElement As IXMLDOMElement
Dim tvwRoot As Node
Dim X As IXMLDOMNodeList
Set m_objDOMPeople = New DOMDocument
'this can stop the m_objDOMPeople object from looking
'for external files which in our case are the people.dtd
'and the people.xsl files - set this to true if you want
'the parser to look for the external files
m_objDOMPeople.resolveExternals = True
'this can stop the m_m_objDOMPeople from validating the XML file
'against the people.dtd file - set this to true if you want validation to
'occur
m_objDOMPeople.validateOnParse = True
'load the XML into the dom document, using a string containing
'the XML location
m_objDOMPeople.async = False
Call m_objDOMPeople.Load(m_strXmlPath)
'check that the load of the XML document was successful
If m_objDOMPeople.parseError.reason <> "" Then
' there has been an error with the loaded XML - show the reason
MsgBox m_objDOMPeople.parseError.reason
Exit Sub
End If
'get the root element of the XML - bypassing the comments, PI's etc
Set objPeopleRoot = m_objDOMPeople.documentElement
'Now lets populate the treecontrol from the DOMDocument
'Set Treeview control properties.
tvwPeople.LineStyle = tvwRootLines
tvwPeople.Style = tvwTreelinesPlusMinusText
tvwPeople.Indentation = 400
'check if the treeview has already been populated - if so
'remove the root, which removes everything.
If tvwPeople.Nodes.Count > 0 Then
tvwPeople.Nodes.Remove 1
End If
' add a child to the root node of the TreeView
Set tvwRoot = tvwPeople.Nodes.Add()
tvwRoot.Text = objPeopleRoot.baseName
'iterate through each element in the dom to fill the tree,
'which in itself iterates through each childNode of that
'element(objPersonElement) to drill down into its childNodes
For Each objPersonElement In objPeopleRoot.childNodes
populateTreeWithChildren objPersonElement
Next
webTarget.Navigate m_strXmlPath
cmdDelete.Enabled = True
cmdClear.Enabled = True
End Sub
我在 w3schools 上找到了 COMMENT_NODE - 8,但我不确定如何在 VB6 中使用它
提前致谢
编辑:使用 Bob 的代码
我是这样称呼它的——这对吗?它遍历并似乎找到了每个 .nodetype BUT 评论。我很确定这是操作员错误 - 如果我错误地调用 DeleteTargetComments - 传递了错误的对象 - 正确的方法是什么?
Private m_objDOMPeople As DOMDocument
Private Sub cmdRemoveComments_Click()
' DeleteTargetComments DOM.documentElement
Dim objPeopleRoot As IXMLDOMElement
Set objPeopleRoot = m_objDOMPeople.documentElement
DeleteTargetComments objPeopleRoot
End Sub
MSXML 中的许多功能确实可以帮助优化在慢速脚本语言中的使用。 XPath 就是其中之一,通常除了复杂性外,它给您带来的好处很少。它在非常长的文档上可能具有的任何性能优势都被完全避免使用 DOM 并简单地转向 SAX 解析所取代。
因此,如果您要处理相当小的 XML 文档,VB6 程序可以非常简单地完成此操作。
原文XML:
<doc>
<item>a</item><!-- target to delete -->
<!-- to keep -->
<item>b<!-- target to delete too --></item>
</doc>
您可以只使用:
Private Sub DeleteTargetComments(ByRef Parent As MSXML2.IXMLDOMNode)
Dim Children As MSXML2.IXMLDOMNodeList
Dim Node As MSXML2.IXMLDOMNode
Set Children = Parent.childNodes
'We must check .length because in MSXML the collection iterators are broken:
If Children.length > 0 Then
For Each Node In Children
With Node
If .nodeType = NODE_COMMENT Then
If InStr(LCase$(.Text), "target") Then
Parent.removeChild Node
End If
Else
DeleteTargetComments Node
End If
End With
Next
End If
End Sub
并将其调用为:
DeleteTargetComments DOM.documentElement
结果:
<doc>
<item>a</item><!-- to keep -->
<item>b</item>
</doc>
非常简单,尽管完整的空白保留是另一个主题。