This code works very well, and the only question I have is with the raw text
formatting (not using XSL).
When I open the xml output in notepad, the data is all run together, instead
of being nicely formatted with each value being on a new line.
For example:
<?xml version="1.0"?>
<memo author="Pat Coleman"><to>Carole Poland</to><subject>big layoff
coming</subject><node1/>node1 value<node2/><node3/></memo>
And I would like it to look like this:
<?xml version="1.0" ?>
<memo author="Pat Coleman">
<to>Carole Poland</to>
<subject>big layoff coming</subject>
<node1 />node1 value
<node2 />
<node3 />
</memo>
1. How can I insert a newline after each entry to make my output look like
the second example above?
2. Does it matter? Will this XML process fine for other scripts that use
XMLDOM to access it?
Thanks for your help everyone!
Here is the example script in question:
----------------------------------------------------
Set xmlDoc = CreateObject("Msxml2.DOMDocument")
xmlDoc.appendChild(xmlDoc.createProcessingInstruction("xml","version=""1.0"""))
Set rootElement=xmlDoc.createElement("memo")
xmlDoc.appendChild(rootElement)
rootElement.setAttribute "author","Pat Coleman"
Set theElement=xmlDoc.createElement("to")
Set theElementText=xmlDoc.createTextNode("Carole Poland")
rootElement.appendChild(theElement)
theElement.appendChild(theElementText)
Set theElement=xmlDoc.createElement("subject")
Set theElementText=xmlDoc.createTextNode("big layoff coming")
rootElement.appendChild(theElement)
theElement.appendChild(theElementText)
'Dim docFragment As IXMLDOMDocumentFragment
Set docFragment = xmlDoc.createDocumentFragment()
docFragment.appendChild xmlDoc.createElement("node1")
docFragment.appendChild xmlDoc.createtextnode("node1 value")
docFragment.appendChild xmlDoc.createElement("node2")
docFragment.appendChild xmlDoc.createElement("node3")
MsgBox docFragment.xml
xmlDoc.documentElement.appendChild docFragment
MsgBox xmlDoc.xml
xmldoc.save "test1.xml"
----------------------------------------
> I have this sample XML creation script that I found via Google.
>
> This code works very well, and the only question I have is with the raw text
> formatting (not using XSL).
>
> When I open the xml output in notepad, the data is all run together, instead
> of being nicely formatted with each value being on a new line.
>
> For example:
>
> <?xml version="1.0"?>
> <memo author="Pat Coleman"><to>Carole Poland</to><subject>big layoff
> coming</subject><node1/>node1 value<node2/><node3/></memo>
>
> And I would like it to look like this:
>
> <?xml version="1.0" ?>
> <memo author="Pat Coleman">
> <to>Carole Poland</to>
> <subject>big layoff coming</subject>
> <node1 />node1 value
> <node2 />
> <node3 />
> </memo>
>
> 1. How can I insert a newline after each entry to make my output look like
> the second example above?
A quick and dirty way: by replacing "><" with ">vbCrLf<" like this:
MsgBox Replace( xmlDoc.xml, "><", ">" + vbCrLf + "<" )
(this won't handle <node1> as you specified)
> 2. Does it matter? Will this XML process fine for other scripts that use
> XMLDOM to access it?
No, it shouldn't matter. Yes, other parsers should process your file
successfully.
Rob
"ekkehard.horner" <ekkehar...@arcor.de> wrote in message
news:42fa4e87$0$11750$9b4e...@newsread4.arcor-online.net...
However, the point is that IE will display your xml file for you
in a nicely indented fashion.
In addition there are a number of utilities out there that will
"tidy-up" your xml for you. Most are free. If interested, google:
"tidy xml"
cheers, jw
____________________________________________________________
You got questions? WE GOT ANSWERS!!! ..(but,
no guarantee the answers will be applicable to the questions)
You append the vbCrLf when you create your XML file and it will come out the
way you want it. The only issue I ran into is it's different when you
create a blank node than when you create a populated one because a blank one
will be written as <node/> and a populated one may have other nodes within
it.
<node>
<other>data</other>
</node>
--
Roland Hall
/* This information is distributed in the hope that it will be useful, but
without any warranty; without even the implied warranty of merchantability
or fitness for a particular purpose. */
Technet Script Center - http://www.microsoft.com/technet/scriptcenter/
WSH 5.6 Documentation - http://msdn.microsoft.com/downloads/list/webdev.asp
MSDN Library - http://msdn.microsoft.com/library/default.asp
I wrote this for someone to create an XML log file. The log.xml file it
creates will look the same in notepad as it does in IE except for the size
of the tab character which I believe is 8 spaces in notepad.
Option Explicit
sub prt(str)
wscript.echo str
end sub
function fileList(folder)
dim file, files, ext
ext = ".xml"
for each file in folder.files
If lcase(right(file.name, 4)) = ext then
files = files & file.name & ","
end if
next
fileList = split(left(files,len(files)-1),",")
end function
sub log(strLog)
dim logXML, logtext, arrLogs, arrText
dim appNode, i, iMax, pi, root, node, att, text
set logXML = CreateObject("MSXML2.DOMDocument")
logXML.async = false
logXML.validateOnParse = false
logXML.resolveExternals = false
if fso.FileExists(logFile) Then
logXML.load logFile
else
logXML.loadXML("<logs/>")
set pi = logXML.createProcessingInstruction("xml", "version='1.0'
encoding='UTF-8'")
logXML.insertBefore pi, logXML.firstChild
set pi = nothing
end if
set root = logXML.documentElement
root.appendChild(logXML.createTextNode(vbCrLf & vbTab))
set node = logXML.createElement("log")
root.appendChild(node)
set att = logXML.createAttribute("date")
set text = logXML.createTextNode(now)
att.appendChild(text)
set node = logXML.documentElement.lastChild
node.setAttributeNode(att)
arrLogs = split(strLog,vbCrLf)
iMax = ubound(arrLogs)
if len(arrLogs(iMax)) = 0 then
iMax = iMax - 1
end if
for i = 0 to iMax
node.appendChild(logXML.createTextNode(vbLf & vbTab & vbTab))
set appNode = logXML.createElement("application")
node.appendChild(appNode)
arrText = split(arrLogs(i),",")
set att = logXML.createAttribute("path")
set text = logXML.createTextNode(arrText(0))
att.appendChild(text)
appNode.setAttributeNode(att)
set att = logXML.createAttribute("name")
set text = logXML.createTextNode(arrText(1))
att.appendChild(text)
appNode.setAttributeNode(att)
set att = logXML.createAttribute("version")
set text = logXML.createTextNode(arrText(2))
att.appendChild(text)
appNode.setAttributeNode(att)
next
node.appendChild(logXML.createTextNode(vbCrLf & vbTab))
root.appendChild(logXML.createTextNode(vbCrLf))
logXML.save logFile
set text = nothing
set att = nothing
set appNode = nothing
set node = nothing
set root = nothing
set logXML = nothing
end sub
function queryXML(xmlfiles)
dim sFilePath, sXPath, oXMLDoc, oErr, dApps, oApp, oApps, sApp, i, j
dim sErr, aTests, sTest, aVersions, sVersion, sItem
aTests = split("ADS Version,State City,RCS,Release,Web",",")
aVersions = split("2.7,3.9.0,9.11.0,3.5.009,5.13.0",",")
for i = 0 to ubound(xmlfiles)
sFIlePath = strPath & "\" & xmlfiles(i)
sXPath = "/Versioning/Application"
Set oXMLDoc = CreateObject("MSXML2.DOMDocument")
oXMLDoc.SetProperty "SelectionLanguage", "XPath"
oXMLDoc.Async = False
oXMLDoc.Load sFilePath
If (oXMLDoc.parseError.errorCode <> 0) Then
Set oErr = oXMLDoc.ParseError
WScript.Echo "Could not load file " & sFilePath _
& " , error: " & oErr.Reason
WScript.Quit
End If
Set dApps = CreateObject("Scripting.Dictionary")
dApps.CompareMode = vbTextCompare
Set oApps = oXMLDoc.DocumentElement.SelectNodes(sXPath)
For Each oApp in oApps
dApps.Add oApp.getAttribute("Name"), oApp.getAttribute("Version")
Next
for j = 0 to ubound(aTests)
sTest = aTests(j)
sVersion = aVersions(j)
sItem = dApps.Item(sTest)
if sItem <> sVersion Then
sErr = sErr & sFilePath & "," & sTest & "," & dApps.Item(sTest) &
vbCrLf
end if
next
next
queryXML = sErr
set oApps = nothing
set dApps = nothing
set oErr = nothing
set oXMLDoc = nothing
end function
Const strPath = "c:\b"
Const logFile = "c:\b\log\xmllog.xml"
dim fso, folder, xmlFiles, errs
set fso = CreateObject("Scripting.FileSystemObject")
set folder = fso.GetFolder(strPath)
xmlFiles = fileList(folder)
errs = queryXML(xmlFiles)
if len(errs) > 0 Then
prt "Errors found!"
prt "Log written to " & logFile
log errs
else
prt "No errors found!"
end if
set fso = nothing
set folder = nothing
This is the file it creates:
<?xml version="1.0" encoding="UTF-8"?>
<logs>
<log date="1/28/2005 2:06:59 AM">
<application path="c:\b\application.xml" name="RCS" version="9.10.8"/>
<application path="c:\b\application.xml" name="Web" version="5.12.9"/>
<application path="c:\b\test1.xml" name="ADS Version" version="2.5"/>
<application path="c:\b\test1.xml" name="State City" version="3.8.0"/>
<application path="c:\b\test2.xml" name="Web" version="4.13.0"/>
</log>
<log date="1/28/2005 2:07:19 AM">
<application path="c:\b\application.xml" name="RCS" version="9.10.8"/>
<application path="c:\b\application.xml" name="Web" version="5.12.9"/>
<application path="c:\b\test1.xml" name="ADS Version" version="2.5"/>
<application path="c:\b\test1.xml" name="State City" version="3.8.0"/>
<application path="c:\b\test2.xml" name="Web" version="4.13.0"/>
</log>
<log date="1/28/2005 2:08:47 AM">
<application path="c:\b\application.xml" name="RCS" version="9.10.8"/>
<application path="c:\b\application.xml" name="Web" version="5.12.9"/>
<application path="c:\b\test1.xml" name="ADS Version" version="2.5"/>
<application path="c:\b\test1.xml" name="State City" version="3.8.0"/>
<application path="c:\b\test2.xml" name="Web" version="4.13.0"/>
</log>
</logs>
HTH...
Rob
"Roland Hall" <nobody@nowhere> wrote in message
news:uD8IU5gn...@tk2msftngp13.phx.gbl...
You're welcome. You have to look at your source to see the effect or look
at the XML file in notepad, not IE.
Good luck.