简体   繁体   中英

xml dom save causing issue with xml file

I've written a vbscript that injects a new Group and User into FileZilla Server.xml from a template - replacing group name and user name with a parameter... restart the service and hey presto this appears within filezilla server as a new account/assigned folders...

Just simple opentextfile, read until < /GROUPS > or < /USERS >, inject the code, save to a new file name. Works fine.

I've then written some code to remove these Group and User elements if it should be required... using XML DOM. It works fine, it does the job... however, when I then come to insert a new record (as above), it cannot write to the XML file as it can no longer locate < /GROUPS > or < /USERS >... I can add wscript.echo to display the lines that are being processed, these elements appear... but are not actioned, like it cannot see them.

Is there anything wrong with how the XML file is saved? It appears fine in notepad. Anythign related to UTF or ASCII ? I'm wondering whether to remove the elements akin to how I'm adding them... read until a point then skip over the lines while writing them out to another file.

Code: checking for the element (also tried Instr, and this couldn't find it after being updated by XMLDOM Save.

Do Until f1.AtEndOfStream
    strLine = f1.ReadLine
    If trim(strLine) = "</Groups>" Then
        If GroupExists="False" Then
            Set f2 = objFSO.OpenTextFile(templatepath&filetemplate,1)

. . . . .

Set objXMLDoc = CreateObject("Microsoft.XMLDOM") 
objXMLDoc.async = False 
objXMLDoc.load(filezillapath&filezilla)

Set objRoot = objXMLDoc.documentElement 
Set objLst  = objXMLDoc.selectSingleNode("//Groups/Group[@Name='" & strNewAccount & "']")
If objLst.Length > 0 Then
    objLst.parentNode.removeChild(objLst)
    objXMLDoc.Save(filezillapath&filezilla)
End If

Set objRoot = objXMLDoc.documentElement 
Set objLst  = objXMLDoc.selectSingleNode("//Users/User[@Name='" & strNewAccount & "']")
'If objLst.Length > 0 Then
    objLst.parentNode.removeChild(objLst)
    objXMLDoc.Save(filezillapath&filezilla)
'End If

Set objXMLDoc = Nothing

Your

Set objLst  = objXMLDoc.selectSingleNode("//Groups/Group[@Name='" & strNewAccount & "']")
If objLst.Length > 0 Then

looks fishy. According to the docs (cf. this too), selectSingleNode() returns a node or Nothing. So try

Set ndX = objXMLDoc.selectSingleNode(...)
If ndX Is Nothing Then
   message
Else
   action
End If

BTW:

  • don't use // if /root/where/ever works
  • VBScript isn't Lisp - no param list () when calling a Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM