PDA

View Full Version : Writing new nodes to XML File


jabaran
10-23-2006, 08:22 AM
I'm probably missing something simple here but I would like to use update an xml file, not nessecarily existing nodes, but add new nodes to the structure. Is there a way of doing this? I see adding attributes isn't a problem but can't seem to figure out how to add children.

Thank you in advance.

jabaran
10-23-2006, 08:29 AM
I think http://www.kinook.com/Forum/showthread.php?threadid=427 might provide enough of a sample. But if you have another way I'd be happy to read it!

jabaran
10-25-2006, 01:28 PM
I ended up with a script that looked like list....

' script to update value attributes in a .NET config file
' requires MSXML v3.0+ to be installed --
' http://msdn.microsoft.com/library/default.asp?url=/downloads/list/xmlgeneral.asp

' load the settings xml document
Set msxml = CreateObject("MSXML2.DOMDocument")
msxml.async = False
msxml.load "%XML_FILE%"

Set root = msxml.documentElement
if root is nothing then
set root = msxml.createElement("Builds")
msxml.appendChild(root)
end if

'Get the node that contains the information about the last build
Set lastBuild = root.lastChild
if not (lastBuild is nothing) then
call lastbuild.setAttribute("NextBuildDate", "%Date%")
set buildNo = lastBuild.getAttributeNode("BuildNo")
if buildNo is nothing then
buildNo = -1
else
buildNo = buildNo.value + 1
end if
else
buildNo = 0
end if

'Create a new Release
Set thisRelease=msxml.createElement("Build")
call thisRelease.setAttribute("BuildNo", buildNo)
call thisRelease.setAttribute("BuildDate", "%dateTime%")
'Next build hasn't happened... set it off in the future... way off into the future...

call thisRelease.setAttribute("NextBuildDate", "12/31/9999")

'Append the new Release
root.appendChild(thisRelease)
' save the changes back out
msxml.save "%XML_FILE%"

The XML ends up looking like...
<Builds>
<Build BuildNo="1" BuildDate="10/25/2006 2:26:54 PM" NextBuildDate="12/31/9999">
</Builds>

Maybe someone will find this useful...