使用vba搜索节点的内容并删除xml文件中的父节点

时间:2016-11-15 13:38:41

标签: excel vba excel-vba

我有一个需要修改的xml文件“sample.xml”。我在Excel工作表的A列中有大约300个字符串的列表,我需要打开XML文件并搜索这些字符串(它将是子节点的内容),如果找到我想要删除它的父节点

这是我的sample.xml,它比我下面提到的文件大,我发布了它的一部分

<gera_it>
<input>
  <Servers>
    <Server>
      <Name>htp</Name>
      <link>1.2.56.89</link>
    </Server>
     <Server>
      <Name>wty</Name>
      <link>1.4.67.89</link>
    </Server>
    <Server>
      <Name>vnb</Name>
      <link>1.6.11.98</link>
    </Server>
    <Server>
      <Name>mnf</Name>
      <link>1.4.89.45</link>
    </Server>
    <Server>
      <Name>typ</Name>
      <link>1.2.44.60</link>
    </Server>
  </Servers>
  <config>
     <map>yes</map>
  </config>
</input>
</gera_it>

我的Excel工作表包含A列中大约300行数据的数据。这些字符串是<Name> </Name>的内容。我在下面提到了一些

wty
mnf
uyt
ifh

我想在sample.xml文件中搜索这些字符串,如果找到字符串,我想删除<Server> </Server>,即它的整个父节点。

这是我现在所拥有的

Const Frow As Long = 3
Const Lrow As Long = 206
Const Stringcol As String = "A"
Dim varStrings As Variant
Dim Fpath As String
Dim ws As Worksheet
Dim lngIndex As Long

Fpath = ThisWorkbook.Path & "\sample.xml"

Set XDoc = CreateObject("MSXML2.DOMDocument")
XDoc.async = False: XDoc.validateOnParse = False
XDoc.Load (Fpath)

Set ws = ActiveSheet

With ws
    ' Create the strings array from the given range value.
    varStrings = .Range(.Cells(Frow, Stringcol), .Cells(Lrow, Stringcol)).Value
    ' Transpose the strings array into a one dimentional array.
    varStrings = Application.WorksheetFunction.Transpose(varStrings)
End With

Set objFileSystemObject = CreateObject("Scripting.FileSystemObject")

For lngIndex = LBound(varStrings) To UBound(varStrings)
    If Len(Trim$(varStrings(lngIndex))) > 0 Then
        String = varStrings(lngIndex)
        XPath = "//gera_it/input/Servers/Server[Name = '" & String & "']"
        'delete child nodes that matches array strings
        For Each Node In XDoc.SelectNodes(XPath)
            Node.ParentNode.Removechild (Node)
        Next
    Else
        'Do Nothing
    End If
Next

XDoc.Save ThisWorkbook.Path & "\sample.xml"

如果我执行上面的操作,我将获得一个空的sample.xml,我不确定它出错的地方。

有人可以帮助我。

1 个答案:

答案 0 :(得分:2)

首先,我认为您的查询字符串正在选择名为Name的节点,而您可能希望选择名为Server的父节点,因为您要从父节点中删除此节点。在下面的代码中,我给出了一些示例语法来实现这一点。

其次,我已将整个查询解析为一个搜索字符串。然后,在您的示例数据中,查询如下所示:

  

// gera_it / input / Servers / Server [Name ='wty'或'mnf'或'uyt'或'ifh']

如果你喜欢那条路线,没有什么可以阻止你单独选择每个节点名称。您只需遍历codes数组并在每个值上调用SelectNodes(并删除)。

Dim xmlDoc As MSXML2.DOMDocument60
Dim nodes As MSXML2.IXMLDOMNodeList
Dim node As MSXML2.IXMLDOMNode
Dim rng As Range
Dim codes As Variant
Dim queryString As String
Dim filePath As String
Dim fileName As String


'Read the codes from column "A"
With Sheet1 'change to your sheet
    Set rng = .Range(.Cells(1, "A"), .Cells(.Rows.Count, "A").End(xlUp))
End With
codes = Application.Transpose(rng.Value2)

'Create your query string
queryString = "//gera_it/input/Servers/Server[Name = '" & _
            Join(codes, "' or '") & "']"

'Load the xml document
filePath = "[your path]"
fileName = "[your filename]"
Set xmlDoc = New MSXML2.DOMDocument60
With xmlDoc
    .async = False
    .validateOnParse = False
    .Load (filePath & "\" & fileName)
End With

'Delete the Server nodes
Set nodes = xmlDoc.SelectNodes(queryString)
For Each node In nodes
    node.ParentNode.RemoveChild node
Next