-1

我一直在网上寻找一种干净的方法来使用 VBS 更改 XML 节点中的 2 个属性(不幸的是,我对这个客户端的唯一选择)。XML 中有一个节点,如下所示:

<lot id="ajhgkhga" lot="1" block="1" section="73R">

我需要将其更改为此(并保留 XML 文档的其余部分):

<lot id="ajhgkhga" lot="1" block="22" section="55">

我一天中的大部分时间都在破解 VBS,这就是我现在所处的位置:

    Dim objXMLDoc
Dim objXMLElement
Dim objXMLNodeList
Dim numObjXMLNodeList
Dim i
Dim lot
Dim section
Dim attr


set objXMLDoc = CreateObject("Microsoft.XMLDOM")

objXMLDoc.async = "false"
objXMLDoc.load("section73R.xml")

Set objXMLNodeList = objXMLDoc.getElementsByTagName("lot")

numObjXMLNodeList = objXMLNodeList.length

For i = 0 to numObjXMLNodeList - 1

    ' current value of the block and section attributes of the lot element
    block = objXMLNodeList.item(i).getAttribute("block")
    section = objXMLNodeList.item(i).getAttribute("section")
    'Wscript.Echo block & " " & section

    ' new value of the block and section attributes of the lot element
    objXMLNodeList.item(i).setAttribute "block", "22"
    objXMLNodeList.item(i).setAttribute "section", "55"

Next

如上所述,这会引发错误。我的 VBS 印章在这一点上几乎不存在,我已经很长时间没有这样做了。上面的代码只是为了尝试读取属性而设计的,我认为一旦我有了替换值就不会太难了。

谁能帮我弄清楚如何替换这些值?这样做告诉我我在这个脚本中有多远?提前致谢!

编辑:我现在可以取出两个属性值(参见上面的代码更改)。我现在需要的只是一种将新属性值写入 XML 文件的方法,这将是一个完成的交易。任何人都可以给我任何指示吗?

编辑#2:一旦我能够检索属性值并且我已经编辑了上面的代码,我就能够很快解决它。

4

1 回答 1

1

目前尚不清楚您的文档的结构是什么。我能够使用此代码更改属性。显然,我的文档中没有多个“批次”,因此您可能需要遍历节点以找到要编辑的节点。不过,这应该可以帮助您:

Dim oXML
Set oXML = CreateObject("Microsoft.XMLDOM")
oXML.loadXML("<list><lot id=""ajhgkhga"" lot=""1"" block=""1"" section=""73R""></lot></list>")

Dim ixnLot
Set ixnLot = oXML.selectSingleNode("//list/lot")
ixnLot.Attributes.getNamedItem("block").Text = "22"
ixnLot.Attributes.getNamedItem("section").Text = "55"

'oXML.Save 'You'll need to call the same method to actually save the changes to the file
于 2012-04-27T21:14:46.897 回答