2016-04-06 7 views
0

VB6を削除VB6のDOMDocumentは、特定のコメントを検索し、Win8.1 のMicrosoft XML v3.0の上

私はVB6を使用してXMLファイル内の要素を検索することができ、無駄-Iで検索しましたが、私は検索する方法を見つけ出すことはできませんxmlファイル内の特定のコメントを削除して削除します。

I素晴らしいVB6 XML tutorial見出さ - 私が働いているコードの種類の例として、ツリービュー/ Webブラウザーコントロールにノードをロードするために、次のいくつかのサンプルコード:

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 

IがCOMMENT_NODE発見した - w3schoolsに8私は事前にVB6で

おかげでそれを使用する方法がわからないです

EDIT:使用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

この質問でXPATHがどのように使用されているかを見てください。http://stackoverflow.com/questions/8836590/vb6-select-a-single-node-using-xpath-with-backslashes-underscores – jac

+0

あなたは応答しています - それをチェックします – beginAgain

+0

これは正しいように見えますが、必要以上に「チャット」があります(別に宣言され、設定された変数は必要ありません)。なぜそれがうまくいかないのか分かりません。 – Bob77

答えて

0

MSXMLの多くの機能は、実際には低速スクリプト言語での使用を最適化するのに役立ちます。 XPathはこれらの1つで、しばしば複雑さを除いてほとんどあなたには得られません。 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> 

かなり簡単ですが、完全な空白保存は別のテーマです。

+0

Bob77 - 応答していただきありがとうございます - これを確認しますが、まだvb6で問題を解決しようとしています。 MSXML2の使い方と同じように、いったんやり遂げると、私は思っています。編集:私が意味することは、MSXML2を参照する方法を考え出すことです - 私はものを複雑にするかもしれないことを認識しています。 – beginAgain

+0

あなたが何を求めているのか分かりません。 'Microsoft XML、v3.0'または' Microsoft XML、v6.0'への参照を設定することができます(両方とも動作しますが、使用されていない4.0または5.0を使用しようとしません。 )。 – Bob77

+0

私は理解する - thx! – beginAgain

関連する問題