如何使用VBscript删除xml文件中节点之间的文本

时间:2014-01-09 06:13:07

标签: xml vbscript

我想在以下xml文件中删除beacon <InstalledPlugins></InstalledPlugins>之间的文本。 请帮帮我。

                       <OceanApplicationShell>
                        <ApplicationShell>
                       <InstalledPlugins>
                        <Plugin>

                    <Author>TechAdvantage</Author>
                    <AppVersion>2012.1</AppVersion>                 
                    <StartMode>Startup</StartMode>
                    <StartupAction>None</StartupAction>
                    <CodeBase>C:\Program Files\Petrel_2012.3\Extensions
                    <InstallerId>{52956779-2949-42AD-A218-B6BD210F8A2F}

                         </Plugin>
                        </InstalledPlugins>
                        </ApplicationShell>
                        </OceanApplicationShell>

输出应如下所示:

         <OceanApplicationShell>
                    <ApplicationShell>
                       <InstalledPlugins>

                       </InstalledPlugins>
                    </ApplicationShell>
         </OceanApplicationShell>

我想以有条件的方式完成这项任务。即如果"<Author>"只是“TechAdvantage”,那么它应该删除信标“<InstalledPlugins>” & “</InstalledPlugins>”之间的文本,否则它应该保持原样。 这就是我尝试过的。

Set objXMLDoc = Wscript.CreateObject("Microsoft.XMLDOM")

 Dim objxmldoc

    objXMLDoc.async = False



    Set objShell=wscript.CreateObject ("wscript.Shell")

    Set fso = wscript.CreateObject("Scripting.FileSystemObject")

    XMLFile = "path of xmlfile"
    objXMLDoc.load(XMLFile) 
    Set colNodes = objXMLDoc.selectNodes ("/OceanApplicationShell/ApplicationShell/InstalledPlugins/Plugin/Author")
    For Each objXMLDoc in colNodes
       Author = objXMLDoc.getAttribute("Author")& "  " & objXMLDoc.text
    if (objXMLDoc.text = "TechAdvantage") Then
    Set nodes = objXMLDoc.selectNodes("OceanApplicationShell/ApplicationShell/InstalledPlugins")
    For Each node In nodes
     node.parentnode.removeChild(node) 
    Next
    End If
    Next
objXMLDoc.Save (XMLFile)
    Set objShell= Nothing

1 个答案:

答案 0 :(得分:0)

使用仅选择要删除的节点的XPath表达式:

author = "TechAdvantage"
xpath  = "//InstalledPlugins/Plugin/Author[text()='" & author & "']/.."

For Each n In objXMLDoc.SelectNodes(xpath)
  n.parentNode.removeChild n
Next