Merging xml files with maxscript in 3dsMax

It’s a hassle to work with multiple xml-files in maxscript at the same time. However it may be desirable to keep separate xml-files on disk. You can combine these xml-files at runtime into a single xml-file in memory while retaining the separate files on disk. This helps when using xpath for instance to search for specific elements in the xml. When working with multiple xml-files, you need to merge them together before performing an xpath query.

In my case I wanted to keep my xml-files on disk separate and not modify them by merging them to a single file. To make sure xpath worked smoothly I merged the xml-files together in a single file in memory by using the “Import”-function.
Also check out the documentation by microsoft on importing nodes on msdn.

This is a maxscript example

xmlDoc = dotNetObject "system.xml.xmlDocument" --setup the main xml document
tempDoc = dotNetObject "system.xml.xmlDocument" --setup a temporary xml document to load the xml values from the disk-files one by one
arrPath = #("path1","path2","path3") --an array of filepaths to multiple xml-files on disk
for i = 1 to arrPath.count do --iterate over the filepaths
(
tempDoc.load arrPath[i] --load up the xml-file from disk. Store it in the temporary xml document
local theNode = xmlDoc.ImportNode tempDoc.documentElement True --get the main node from the temp doc as an import from the main doc
xmlDoc.documentElement.appendchild theNode --append the import-node
)

This results in an xmlDocument in memory which contains the contents of several xml-files. You can leave it at that and perform your actions on the xmlDocument in memory or you can store that xmlDocument to disk for later use.

xmlDoc.save (@"someNicePath.xml")

For more info on using xpath in maxscript, check out this article on my blog.
For some real xml and maxscript kungfu check out Pete Addington’s site: http://lonerobot.net/
For a nice breakdown of xml in maxscript check out Paul Neale’s tutorials here and here.

Share your thoughts

This site uses Akismet to reduce spam. Learn how your comment data is processed.