Create an XML file from VBS?

Posted on 2006-05-16
Medium Priority
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
  • 4
LVL 44

Expert Comment

ID: 16693009
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
source: http://www.jdhitsolutions.com/resources/scripts/ConvertCSVtoXML.txt

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

Author Comment

ID: 16693042
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

bruintje earned 200 total points
ID: 16693408
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

source: http://builder.com.com/5100-6373-1044759-2.html#Listing%20B

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

ID: 16693424
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

ID: 16877143
this was answered by me

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
The viewer will learn how to count occurrences of each item in an array.
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.
Suggested Courses

807 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