我有一个类似这样的XML文件
<abc>
<ab>value</ab>
<aa>time</aa>
<ac>money</ac>
</abc>
<abc>
<ab>right</ab>
<aa>left</aa>
<ac>straight</ac>
</abc>
我想要的是我能够找到一个值为“left”的节点,然后删除它的父节点,以便我最后得到的是
<abc>
<ab>value</ab>
<aa>time</aa>
<ac>money</ac>
</abc>
提前致谢
答案 0 :(得分:1)
如果通过添加文档元素将其更正为有效的XML:
<doc>
<abc>
<ab>value</ab>
<aa>time</aa>
<ac>money</ac>
</abc>
<abc>
<ab>right</ab>
<aa>left</aa>
<ac>straight</ac>
</abc>
</doc>
您可以执行以下操作:
Dim DOM As MSXML2.DOMDocument
Dim Node As MSXML2.IXMLDOMNode
Set DOM = New MSXML2.DOMDocument
With DOM
.async = False
.preserveWhiteSpace = True
If .Load("sample.xml") Then
.setProperty "SelectionLanguage", "XPath"
Set Node = .selectSingleNode("//*[.='left']")
If Not Node Is Nothing Then
.documentElement.removeChild Node.parentNode
On Error Resume Next
Kill "sample.xml"
On Error GoTo 0
.save "sample.xml"
MsgBox "Done, saved as sample.xml"
Else
MsgBox "No ""left"" found"
End If
Else
MsgBox "Load failed!" & vbNewLine & vbNewLine _
& CStr(.parseError.errorCode) & " " _
& .parseError.reason
End If
End With