Win8.1上的VB6 Microsoft 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但是注释。我很确定这是操作符错误 - 如果我错误地调用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
答案 0 :(得分:0)
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>
非常简单,尽管完整的空白保存是另一个主题。