Re: Create XML File with VBSCRIPT by ekkehard
ekkehard
Tue Aug 16 12:48:00 CDT 2005
Scott Morgan wrote:
> I am new to the XML world and wanted to see if anyone had a simple sample
> vbscript that will create an XML file with data from parameters passed to a
> function. Hope that makes sense. I need it to create the file, open the
> file, writed the tags and close the file to a specific path. Any and all
> help is greatly appreciated.
>
> Thanks in advance for your help.
>
>
Depending on the data you want to write and the structure of the
resulting .xml file you may get away with something simple like
----------------------------------------------------
Dim sFSpec ' FSpec of file to write
Dim sXmlPfx ' fixed xml prefix
Dim sXmlSfx ' fixed xml suffix
Dim aMeta ' fixed meta/tag info
Dim aData ' changing data
' needs your attention
sFSpec = ".\test.xml"
sXmlPfx = "<?xml version = '1.0' encoding = 'ISO-8859-1' standalone = 'yes'?>"
sXmlPfx = sXmlPfx + "<library>"
sXmlSfx = "</library>"
aMeta = Array( "book", "author", "title" )
aData = Array( Array( "Bakken, Torgeir", "All Dark Secrets of VBScript" ) _
, Array( "Harris, Michael", "Doing XML with VBScript" ) _
)
' should work as is
Dim sXML
Dim aItem
Dim nIdx
sXmlPfx = Replace( sXmlPfx, "'", """" ) ' not without risk of replacing
sXmlSfx = Replace( sXmlSfx, "'", """" ) ' "'"s you want to keep
sXML = sXmlPfx
For Each aItem in aData
sXML = sXML + "<" + aMeta( 0 ) + ">"
For nIdx = 1 To UBound( aMeta )
sXML = sXML + "<" + aMeta( nIdx ) + ">"
sXML = sXML + aItem( nIdx - 1 )
sXML = sXML + "</" + aMeta( nIdx ) + ">"
Next
sXML = sXML + "</" + aMeta( 0 ) + ">"
Next
sXML = sXML + sXmlSfx
sXML = Replace( sXML, "><", ">" + vbCrLf + "<" ) ' poor man's format
CreateObject( "Scripting.FileSystemObject" ) _
.CreateTextFile( sFSpec, True ) _
.Write sXML
----------------------------------------------------
to get
----------------------------------------------------
<?xml version = "1.0" encoding = "ISO-8859-1" standalone = "yes"?>
<library>
<book>
<author>Bakken, Torgeir</author>
<title>All Dark Secrets of VBScript</title>
</book>
<book>
<author>Harris, Michael</author>
<title>Doing XML with VBScript</title>
</book>
</library>
----------------------------------------------------
As you see - no need to consult the MS XML SDK documention
to get a (very simple) job done. If things get more complicated
(attributes, deep(er) structure, ...) you should do just this:
consult the MS XML SDK documention. To get you started, I include
code to build your library with "Msxml.DOMDocument".
----------------------------------------------------
Dim sFSpec ' FSpec of file to write
Dim aMeta ' fixed meta/tag info
Dim aData ' changing data
' needs your attention
sFSpec = ".\test2.xml"
aMeta = Array( "library", "book", "author", "title" )
aData = Array( Array( "Bakken, Torgeir", "All Dark Secrets of VBScript" ) _
, Array( "Harris, Michael", "Doing XML with VBScript" ) _
)
' should work as is
Dim oXDOM
Dim ndRoot
Dim aItem
Dim ndItem
Dim ndTag
Dim nIdx
Set oXDOM = CreateObject( "Msxml.DOMDocument" )
oXDOM.documentElement = oXDOM.createElement( aMeta( 0 ) )
Set ndRoot = oXDOM.documentElement
oXDOM.insertBefore oXDOM.createProcessingInstruction( "xml", "version = ""1.0""" ) _
, ndRoot
For Each aItem in aData
Set ndItem = oXDOM.createElement( aMeta( 1 ) )
For nIdx = 2 To UBound( aMeta )
Set ndTag = oXDOM.createElement( aMeta( nIdx ) )
ndTag.appendChild oXDOM.createTextNode( aItem( nIdx - 2 ) )
ndItem.appendChild ndTag
Next
ndRoot.appendChild ndItem
Next
oXDOM.Save CreateObject( "Scripting.FileSystemObject" ) _
.GetAbsolutePathName( sFSpec )
----------------------------------------------------