Generating XML file from script
I recently had a reason to include the creation of an XML configuration file in our script collection. The easiest way to do this is to hard-code the XML content as a string in a script, then just create a file and write that XML string to a file (named appropriately, etc.). Is there a more elegant or efficient way to do this?
The script line I want to change is written in VBScript.
source to share
Very important to read if you are considering strings: HOWTO Avoid being called Bozo when creating XML
source to share
My final solution: adapted the XMLWriter mentioned in divo (he used it pretty much as it is, apart from one or two changes, since this script needs to run on the client). Also adapted the code here to print the XML nicely (so that it can be read when exiting to the file):
Function XMLToString(Nodes)
dim retStr
retStr = "<?xml version=""1.0"" encoding=""utf-8"" ?>"
XMLToString = retStr & vbNewLine & NodesToString(Nodes, 0)
End Function
Function NodesToString(Nodes, Indent)
Dim xNode
Dim retStr
retStr = ""
For Each xNode In Nodes
Select Case xNode.nodeType
Case 1: ' NODE_ELEMENT
If xNode.nodeName <> "#document" Then
' change DisplayAttrs_(xNode, Indent + 2) to
' DisplayAttrs_(xNode, 0) for inline attributes
retStr = retStr & VBNewLine & strDup(" ", Indent) & "<" & xNode.nodeName & AttributesToString(xNode) & ">"
If xNode.hasChildNodes Then
retStr = retStr & NodesToString(xNode.childNodes, Indent + 2)
End If
retStr = retStr & VBNewLine & strDup(" ", Indent) & "</" & xNode.nodeName & ">"
Else
If xNode.hasChildNodes Then
retStr = retStr & NodesToString(xNode.childNodes, Indent + 2)
End If
End If
Case 3: ' NODE_TEXT
retStr = retStr & VBNewLine & strDup(" ", Indent) & xNode.nodeValue
End Select
Next
NodesToString = retStr
End Function
Function AttributesToString(Node)
Dim xAttr, res
res = ""
For Each xAttr In Node.attributes
res = res & " " & xAttr.name & "=""" & xAttr.value & """"
Next
AttributesToString = res
End Function
Function strDup(dup, c)
Dim res, i
res = ""
For i = 1 To c
res = res & dup
Next
strDup = res
End Function
source to share