Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: Need help adding element to existing XML file using VBScript  (Read 12703 times)

0 Members and 1 Guest are viewing this topic.

gsnidow

    Topic Starter


    Rookie

  • Just a guy trying to make work stuff easier
    • Experience: Beginner
    • OS: Unknown
    Need help adding element to existing XML file using VBScript
    « on: December 12, 2013, 06:05:56 AM »
    Greetings all.  I'm using Windows 7 32, and am trying to add a node element to an existing and simple XML document.  What I am trying to do is store a filename and a filepath as defaults for an FTP application I'm making.  So, for example, if user needs to FTP a file called 'File1' to local directory 'filepath1' on a regular basis, they will simply choose the filename from a list (which is already working), and pick a default filepath from a list (which is also already working), and click a button to store the filename and file path in an XML document so the next time they need to FTP that file, they can simply click a button without having to choose the lcd every time.  Using too many "how to" sites to list, I got the below to create the XML file if it does not already exist, but honestly, I don't know if this is the best way to accomplish what I need to do.
    Code: [Select]
    set objFSO = CreateObject("Scripting.FileSystemObject")
    set xmlDoc = CreateObject("Microsoft.XMLDOM")
    strFile = "u:\xml\default_mapping.xml"

    If Not (objFSO.FileExists(strFile)) Then
    set objRoot = xmlDoc.createElement("default_mapping")
    xmlDoc.appendChild objRoot

    set objFname = xmlDoc.createElement("FileName")
    objRoot.appendChild objFname
    objFname.text = "File1" 'this will be a variable passed by calling sub
    objFname.setAttribute "SavePath", "filepath1" 'this will be a variable passed by calling sub

    set objIntro = xmlDoc.createProcessingInstruction("xml","version='1.0'")
    xmlDoc.insertBefore objIntro, xmlDoc.childNodes(0)
    xmlDoc.Save strFile
    Else
    'Don't know what to do here to add another file
    End if

    Now, this all fires upon the user choosing the filename and filepath from two list boxes, which are passed as variables where here I have hard coded 'File1' and 'filepath1'.  If the file does not exist, the above code creates XML file 'default_mapping.xml', which looks like this...

    Code: [Select]
    <?xml version="1.0"?>
    <default_mapping><FileName SavePath="filepath1">File1</FileName></default_mapping>

    My problem is that I need to be able to be able to add more elements when the user wants to store another file and filepath.  I found lots of help with creating the file, but if I saw anything about adding elements, I did not know what I was looking at.  I'm comfortable with VBScript, but XML is new, so I don't have a good grasp of terms describing XML.  I *think* in the above example FileName is a child element of the default_mapping node, and SavePath is an attribute of that element. If the XML file already exists,  I need to know how to add 'File2' and 'filepath2', and 3, ad infinitum.  Does anyone know of a link that will describe how to do this?  Thank you.

    Greg

    Sidewinder



      Guru

      Thanked: 139
    • Experience: Familiar
    • OS: Windows 10
    Re: Need help adding element to existing XML file using VBScript
    « Reply #1 on: December 14, 2013, 06:27:51 AM »
    In keeping with your XML structure, the root element name was changed to Mapping (this seemed more sensible with all the FileName elements dumped into the root) and an attribute (Usage=Default) was added to the first FileName element. As you found out, creating an XML file requires creating a root element (default_mapping), a parent element (not used here) and then the child elements. And don't forget the XML declarative as the first line (used here but optional). It is all done in memory then written to disk. You did all that with your posted code.

    The flip side of the if statement is for when the XML file already exists. Here you load the existing file into memory (xmlDoc has a load method), find where to insert the new node (in this case within the root element, create the new node and finally write everything to disk.

    I changed your code into a subroutine to show how to get external data into the xmlDoc. These passed variables go by the name of dhPath and dhFile.

    Code: [Select]
    Sub XmlMaint(dhPath, dhFile)
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set xmlDoc = CreateObject("Microsoft.XMLDOM")

    strFile = "u:\xml\default_mapping.xml"

    If Not (objFSO.FileExists(strFile)) Then
      Set objIntro = xmlDoc.createProcessingInstruction("xml","version='1.0'")
    xmlDoc.insertBefore objIntro, xmlDoc.childNodes(0)

    Set objRoot = xmlDoc.createElement("Mapping")
    xmlDoc.appendChild objRoot

    Set objFname = xmlDoc.createElement("FileName")
    objRoot.appendChild objFname

    objFname.text = dhFile                         'variable from caller
    objFname.setAttribute "Usage", "Default"
    objFname.setAttribute "SavePath", dhPath       'variable from caller

    xmlDoc.Save strFile
    Else
      xmlDoc.async = False
      xmlDoc.load(strFile)
     
      Set objRoot = xmlDoc.documentElement            'grab root; start insertion
      Set objFname = xmlDoc.createElement("FileName")
      objRoot.appendChild objFname
     
      objFname.text = dhFile                          'variable from caller
      objFname.setAttribute "SavePath", dhPath        'variable from caller
     
      xmlDoc.save strFile
    End if
    End Sub

    Call XmlMaint("u:\temp", "ComputerHope.ftp")

    The call to the subroutine at the end shows how to pass variables into the subroutine (dhPath is first, dhFile is second) The FileName element with the Usage=Default attribute can easily be identified. If you haven't seen it yet, this may be helpful.

    And So It Goes.  8)

    Note: VBScript creates the XML code as one long string. The Foxe and Notepad++  editors can tidy your XML code for readability. Notepadd++ uses a plugin you can get from the built-in plugin manager (called tidy2). Both are free.
    « Last Edit: December 14, 2013, 06:39:26 AM by Sidewinder »
    The true sign of intelligence is not knowledge but imagination.

    -- Albert Einstein

    gsnidow

      Topic Starter


      Rookie

    • Just a guy trying to make work stuff easier
      • Experience: Beginner
      • OS: Unknown
      Re: Need help adding element to existing XML file using VBScript
      « Reply #2 on: December 19, 2013, 05:08:43 PM »
      Thank you Sidewinder.  It is going to take a while for me to get back to this with everyone being out for Xmas and all, but I'll definitely post back with results in a few days.  Thanks again.

      Greg

      gsnidow

        Topic Starter


        Rookie

      • Just a guy trying to make work stuff easier
        • Experience: Beginner
        • OS: Unknown
        Re: Need help adding element to existing XML file using VBScript
        « Reply #3 on: December 20, 2013, 07:26:44 AM »
        Sidewinder, thank you so much, your explanation proved to be more helpful than everything I read.  Ironically, I had just finished reading the the link you referenced when I decided it was time to ask for help.  I think what was confusing me the most is that I did not understand that documentElement was a reference to the root element.  I kept seeing code where someones was looking for a named element, and I could not get it to work for me.  Anyhow, thanks again, it worked like a charm and has made me realize there is much more to learn. 

        Greg