使用vbasearchxml文件中节点的内容并删除父节点

我有一个需要修改的XML文件“sample.xml”。 我有一个在我的Excel表中的A列约300string的列表,我需要打开XML文件并search这些string(这将是一个子节点的内容),如果发现我想删除它的父节点。

这里是我的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行数据的数据。 这些string是<Name> </Name> 。 我在下面提到了其中几个

 wty mnf uyt ifh 

我想在sample.xml文件中search这些string,如果findstring,我想要删除它的整个父节点<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,如果我执行上面,我不知道哪里出错了。

有人可以帮我吗?

首先,我认为你的查询string是select名为Name的节点,而你可能想select名为Server的父节点,因为它是你想要从父节点删除的这个节点。 在下面的代码中,我已经给你一些示例语法来实现这一点。

其次,我已经把整个查询分解成一个searchstring。 在您的示例数据中,查询如下所示:

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

没有什么可以阻止你单独select每个节点的名字,如果你喜欢走这条路线。 您只需循环访问codes数组,然后在每个值上调用SelectNodes (和remove)。

 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