Create an XML file from VBS?

What is the manner in which a .xml file is created in VBS?

Any assistance is greatly appreciated.
Who is Participating?
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"
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
davismAuthor Commented:
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.
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>
this was answered by me
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.