替换XML属性值

替换XML属性值,xml,vbscript,nodes,xmlnode,Xml,Vbscript,Nodes,Xmlnode,在过去的几天里,我一直在阅读有关使用XML文件的文章,我对此感到非常惊讶 获取属性并更改值似乎很简单,但我无法做到这一点 我有一个名为input.XML的XML文件: <gs:GlobalizationService xmlns:gs="urn:longhornGlobalizationUnattend"> <gs:UserList> <gs:User UserID="Current"/> </gs:UserList>

在过去的几天里,我一直在阅读有关使用XML文件的文章,我对此感到非常惊讶

获取属性并更改值似乎很简单,但我无法做到这一点

我有一个名为input.XML的XML文件:

<gs:GlobalizationService xmlns:gs="urn:longhornGlobalizationUnattend">
    <gs:UserList>
        <gs:User UserID="Current"/>
    </gs:UserList>
    <gs:InputPreferences>
        <gs:InputLanguageID Action="add" ID="0409:00000409" Default="true"/>
    </gs:InputPreferences>
</gs:GlobalizationServices>

您应该使用XPath选择器执行此操作,然后编译选择器。我认为您不需要使用任何第三方库。我认为JDK有你需要做的东西。如果你用谷歌搜索它,会有一些例子。

(1)使用带有基本错误检查的框架,如

Dim oFS    : Set oFS  = CreateObject("Scripting.FileSystemObject")
Dim sFSpec : sFSpec   = oFS.GetAbsolutePathName("...")
Dim oXML   : Set oXML = CreateObject("Msxml2.DOMDocument")
oXML.setProperty "SelectionLanguage", "XPath"
' If namespace(s)
' oXML.setProperty "SelectionNamespaces", "...'"
oXML.async = False
oXML.load sFSpec
If 0 = oXML.parseError Then
   WScript.Echo oXML.xml
   WScript.Echo "-----------------"
   Dim sXPath : sXPath    = "..."
   Dim ndFnd  : Set ndFnd = oXML.selectSingleNode(sXPath)
   If ndFnd Is Nothing Then
      WScript.Echo sXPath, "not found"
   Else
      WScript.Echo ndFnd.nodeName, ndFnd.getAttribute("UserID")
      WScript.Echo "-----------------"
      ndFnd.setAttribute "UserID", "Changed"
      WScript.Echo oXML.xml
      oXML.save Replace(sFSpec, ".xml", "-o.xml")
   End If
Else
   WScript.Echo oXML.parseError.reason
End If
(2) 在填写文件规范之后,您会发现您的XML格式不正确

script 13589885.vbs
nd tag 'gs:GlobalizationServices' does not match the start tag 'gs:GlobalizationService'.
(3) 由于XML包含一个或多个命名空间,因此需要

oXML.setProperty "SelectionNamespaces", "xmlns:gs='urn:longhornGlobalizationUnattend'"
...
Dim sXPath : sXPath    = "/gs:GlobalizationService/gs:UserList/gs:User"
(4) 现在它没有明显的故障:

cscript 13589885.vbs
<gs:GlobalizationService xmlns:gs="urn:longhornGlobalizationUnattend">
        <gs:UserList>
                <gs:User UserID="Current"/>
        </gs:UserList>
        <gs:InputPreferences>
                <gs:InputLanguageID Action="add" ID="0409:00000409" Default="true"/>
        </gs:InputPreferences>
</gs:GlobalizationService>

-----------------
gs:User Current
-----------------
<gs:GlobalizationService xmlns:gs="urn:longhornGlobalizationUnattend">
        <gs:UserList>
                <gs:User UserID="Changed"/>
        </gs:UserList>
        <gs:InputPreferences>
                <gs:InputLanguageID Action="add" ID="0409:00000409" Default="true"/>
        </gs:InputPreferences>
</gs:GlobalizationService>
cscript 13589885.vbs
-----------------
用户电流
-----------------
(5) 思考一下为什么
xmldoc.Load(“input.xml”)
xmldoc.save(“input.xml”)
是糟糕的VBScript

cscript 13589885.vbs
<gs:GlobalizationService xmlns:gs="urn:longhornGlobalizationUnattend">
        <gs:UserList>
                <gs:User UserID="Current"/>
        </gs:UserList>
        <gs:InputPreferences>
                <gs:InputLanguageID Action="add" ID="0409:00000409" Default="true"/>
        </gs:InputPreferences>
</gs:GlobalizationService>

-----------------
gs:User Current
-----------------
<gs:GlobalizationService xmlns:gs="urn:longhornGlobalizationUnattend">
        <gs:UserList>
                <gs:User UserID="Changed"/>
        </gs:UserList>
        <gs:InputPreferences>
                <gs:InputLanguageID Action="add" ID="0409:00000409" Default="true"/>
        </gs:InputPreferences>
</gs:GlobalizationService>