VB6 DOMDocument查找特定注释并删除

VB6 DOMDocument查找特定注释并删除,vb6,comments,domdocument,Vb6,Comments,Domdocument,Win8.1上的VB6 MicrosoftXMLV3.0 我的搜索是徒劳的——我可以使用VB6搜索xml文件中的元素,但我不知道如何搜索xml文件中的特定注释并将其删除 我在下面找到了一个很棒的示例代码,可以将节点加载到treeview/WebBrowser控件中,作为我正在使用的代码类型的一个示例: Private Sub cmdPopulate_Click() Dim objPeopleRoot As IXMLDOMElement Dim objPersonElement As IX

Win8.1上的VB6 MicrosoftXMLV3.0

我的搜索是徒劳的——我可以使用VB6搜索xml文件中的元素,但我不知道如何搜索xml文件中的特定注释并将其删除

我在下面找到了一个很棒的示例代码,可以将节点加载到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

MSXML中的许多特性确实有助于优化在慢速脚本语言中的使用。XPath就是其中之一,通常除了复杂度之外,它对您的帮助很小。它在非常长的文档上可能具有的任何性能优势都会因为完全避免使用DOM而被吹走,只需转而使用SAX解析即可

因此,如果您正在处理相当小的XML文档,VB6程序可以非常简单地完成这项工作

原始XML:

<doc>
    <item>a</item><!-- target to delete -->
    <!-- to keep -->
    <item>b<!-- target to delete too --></item>
</doc>
并将其作为:

DeleteTargetComments DOM.documentElement
结果:

<doc>
    <item>a</item><!-- to keep -->
    <item>b</item>
</doc>

A.
B

非常简单,但完全保留空白是另一个主题。

看看XPATH在这个问题中是如何使用的,Jac,感谢您的回答-将检查它是否正确,尽管比要求的更“健谈”(不需要单独声明和设置变量)。我不知道为什么它不起作用。Bob77-感谢您的回复-将检查这一点-仍然试图在vb6中解决问题。就像使用MSXML2一样——我想,一旦我完成了这项工作,我就可以开始了。编辑:我的意思是想知道如何引用MSXML2-我知道我可能把事情复杂化了。我不确定你在问什么。您可以设置对
Microsoft XML,v3.0
Microsoft XML,v6.0
的引用(这两种方法都可以,但不要尝试使用您看到的过时的和特定于MS Office的任何4.0或5.0)。
DeleteTargetComments DOM.documentElement
<doc>
    <item>a</item><!-- to keep -->
    <item>b</item>
</doc>