【问题标题】:Replace XML attribute value替换 XML 属性值
【发布时间】:2012-11-15 09:47:01
【问题描述】:

在过去的几天里,我一直在阅读有关使用 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>

我需要能够更改属性 ID 的值。似乎我应该能够使用 selectSingleNode 和 setAttribute 命令的组合来完成此操作,但我无法让它工作。

下面是我一直在尝试的一个 sn-p。值根据用户选择在主脚本的其他位置定义。

Dim xmlDoc, xmlNode
Set xmlDoc = CreateObject("Microsoft.XMLDOM")
xmlDoc.Async = "False"
xmldoc.Load("input.xml")

Set xmlNode = xmlDoc.selectSingleNode("gs:GlobalizationServices/gs:InputPreferences/gs:InputLanguageID")
xmlNode.setAttribute "ID", Value
xmlDoc.save("input.xml")

【问题讨论】:

    标签: xml vbscript nodes xmlnode


    【解决方案1】:

    您应该使用 XPath 选择器,然后编译选择器。我认为您不需要使用任何 3rd 方库。我认为 JDK 有你需要做的东西。如果你用谷歌搜索,就会有例子。

    【讨论】:

      【解决方案2】:

      (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 包含 (a) 命名空间,因此您需要

      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>
      

      (5) 反思为什么xmldoc.Load("input.xml")xmlDoc.save("input.xml") 是不好的VBScript。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-04-19
        • 1970-01-01
        • 2022-01-26
        • 1970-01-01
        • 2015-06-01
        • 1970-01-01
        • 2015-02-20
        • 2021-05-18
        相关资源
        最近更新 更多