Create an XML file from VBS?

Posted on 2006-05-16
Last Modified: 2013-11-19
What is the manner in which a .xml file is created in VBS?

Any assistance is greatly appreciated.
Question by:davism
    LVL 44

    Expert Comment

    Hi davism,

    really depends where your data is coming from, i didn't look into it but there is a sample script here to convert csv to xml using vbs

    just comment if you have comments on this, i did a xml to csv using xsl script and was looking for the other way around which looked pretty difficult

    hope this helps a bit
    LVL 1

    Author Comment

    I want to create it from scratch. Not from a recordset or csv file or anything like that. I want to create something from scratch. The source may be different values from different places.
    LVL 44

    Accepted Solution

    that is a bit vague but instead of using fso and just writing lines to a file

    you can also use a vb example and rewrite it for vbs using the MSXML DOM which is available on most windows pc's with IE5+ and when you study it a bit and play with it its also more powerfull then simply writing lines with tags to a file


    1. the vbs version was taken from listing A, B and C from that excellent article
    2. copy this code to a empty notepad file
    3. save it as test.vbs to the desktop
    4. execute it and you'll find a file test.xml on C

    Dim Version '<?xml version="1.0"?>
    Dim CatalogDoc 'xmldocument
    'Create a new document object
    Set CatalogDoc = CreateObject("MSXML.DOMDocument")
    CatalogDoc.preserveWhiteSpace = False
    CatalogDoc.async = False
    'tell the parser to automatically load externally defined DTD's or XSD's
    CatalogDoc.resolveExternals = True

    Set Version = CatalogDoc.createProcessingInstruction("xml", "version=" & Chr(34) & "1.0" & Chr(34))

    'create the root catalog element
    'and append it and the XML version to the document
    CatalogDoc.appendChild Version
    CatalogDoc.appendChild CatalogDoc.createElement("catalog")

    Dim NewBookNode 'New root node for the new book item
    Dim DocFrag 'Document fragment used to build new book item
    Dim valResult

    'create the new book element
    Set NewBookNode = CatalogDoc.createElement("book")
    CatalogDoc.documentElement.appendChild NewBookNode
    'Build the child nodes of the book node and hand

    'Build the document structure needed to add a new book to the catalog
    Dim TitleNode 'Book title
    Dim AuthorNode 'Book Author
    Dim PriceNode 'Book Price
    Dim GenreNode 'Book Genre
    Dim DescNode 'Book Description
    Dim PubDateNode 'Book publish date
    Dim ID 'ID attribute for the book
    Dim CatalogNode 'The document's root catalog element

    'Create the ID attribute node for the new book
    Set ID = CatalogDoc.createAttribute("id")

    'Calculate the value of the new ID attribute
    Set CatalogNode = CatalogDoc.documentElement
    ID.Value = "bk" & CatalogNode.childNodes.length

    'Create all the new data nodes
    Set TitleNode = CatalogDoc.createElement("title")
    Set AuthorNode = CatalogDoc.createElement("author")
    Set PriceNode = CatalogDoc.createElement("price")
    Set GenreNode = CatalogDoc.createElement("genre")
    Set DescNode = CatalogDoc.createElement("description")
    Set PubDateNode = CatalogDoc.createElement("publish_date")

    'add space for text under all the new nodes
    TitleNode.appendChild CatalogDoc.createTextNode("")
    AuthorNode.appendChild CatalogDoc.createTextNode("")
    PriceNode.appendChild CatalogDoc.createTextNode("")
    GenreNode.appendChild CatalogDoc.createTextNode("")
    DescNode.appendChild CatalogDoc.createTextNode("")
    PubDateNode.appendChild CatalogDoc.createTextNode("")

    'Add the new nodes to the root "book" node
    'Note that the append order is important
    NewBookNode.Attributes.setNamedItem ID
    NewBookNode.appendChild AuthorNode
    NewBookNode.appendChild TitleNode
    NewBookNode.appendChild GenreNode
    NewBookNode.appendChild PriceNode
    NewBookNode.appendChild PubDateNode
    NewBookNode.appendChild DescNode

    'Traverse the child nodes of the book element
    'setting the text elements to the appropriate values.
    'Remember that the text contained in an element
    'is considered to be a seperate child node of that

    Dim nod
    Set nod = NewBookNode.firstChild
    nod.firstChild.nodeValue = "This is an Author"
    Set nod = nod.nextSibling
    nod.firstChild.nodeValue = "Books books books"
    Set nod = nod.nextSibling
    nod.firstChild.nodeValue = "Technical Writing"
    Set nod = nod.nextSibling
    nod.firstChild.nodeValue = "$9.95"
    Set nod = nod.nextSibling
    nod.firstChild.nodeValue = "Librarian books"
    Set nod = nod.nextSibling
    nod.firstChild.nodeValue = "This is a book about books"

    CatalogDoc.Save "c:\test.xml"
    LVL 44

    Expert Comment

    resulting xml looked like

    <?xml version="1.0" ?>
       <book id="bk1">
         <author>This is an Author</author>
          <title>Books books books</title>
          <genre>Technical Writing</genre>
          <publish_date>Librarian books</publish_date>
          <description>This is a book about books</description>
    LVL 44

    Expert Comment

    this was answered by me

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
    Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
    Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
    The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now