[Closed] XML Library
I was trying to output data to a XML file, and an ActiveX solution is not my way, so I wrote this simple but useful script for my project.
It doesn’t test correct closing of keys and also doesn’t outputs indenting, but is absolutely valid XML.
I’m not a script developer, so it can have lots of improvements!
the following code:
local XMLData
XMLData = XMLCreate “C: est.xml”
XMLNewKey &XMLData “XMLTest”
XMLAddParam &XMLData “MAXfile” maxFileName
XMLAddParam &XMLData “scale” (units.SystemScale)
XMLNewKey &XMLData “objects”
XMLAddParam &XMLData “count” $geometry.count
XMLCloseKey &XMLData “objects”
XMLCloseKey &XMLData “XMLTest
outputs:
<?xml version=“1.0” encoding=“UTF-8”?>
<XMLTest MAXfile=“Test.max” scale=“0.05”>
<objects count=“99” />
</XMLTest>
I hope this becomes of any use for anyone.
VilaXML.ms (I don’t know if I can attach files!)
fn XMLWriteKey data =
(
– format “XMLWriteKey %
” data[2].count
if data[2].count < 1 then return 0
if data[2].count == 1 then
(
– format “<%>
” data[2][1]
format “<%>
” data[2][1] to:data[1]
)
else
(
– format “<%” data[2][1]
format “<%” data[2][1] to:data[1]
for p = 2 to data[2].count do
(
– format ” %=”%”” data[2][p][1] data[2][p][2]
format ” %=”%”” data[2][p][1] data[2][p][2] to:data[1]
)
– format “>
“
)
)
fn XMLNewKey &data nombre =
(
– Vuelca los datos anteriores
if data[2].count > 0 then
(
XMLWriteKey data
format “>
” to:data[1]
)
data = #(data[1], #(nombre))
)
fn XMLAddParam &data nombre value =
(
params = data[2]
append params #(nombre, value)
data = #(data[1], params)
– format “Params: %
” params
)
fn XMLCloseKey &data nombre =
(
if data[2].count > 0 then
(
XMLWriteKey data
format ” />
” to:data[1]
)
else
(
format “</%>
” nombre to:data[1]
)
data = #(data[1], #())
)
fn XMLCreate file =
(
– format “XMLCreate: %
” file
out_file = openFile file mode:“w”
format “<?xml version=“1.0” encoding=“UTF-8”?>
” to:out_file
data = #(out_file, #())
/* XMLNewKey &data “SandboxObject”
XMLAddParam &data “meshFile” ((getFilenameFile file) + “.3dmsh”)
XMLAddParam &data “collFile” ((getFilenameFile file) + “.3dcol”)
XMLAddParam &data “MAXfile” maxFileName
XMLAddParam &data “scale” (units.SystemScale)
XMLWriteKey data
format “>
” to:data[1]
*/
#(out_file, #())
)
fn XMLClose data =
(
– format “XMLClose % %
” data[2] data[2].count
data = #(data[1], #())
– XMLCloseKey &data “SandboxObject”
– fflush data[1]
close data[1]
)
For XML in maxscript, I just use the .NET xml classes. That is if you remember, in release 9 of Max, the entire friggen .NET framework class libraries were exposed to maxscript, including their excellent XML classes. I used those to port some old maxscripts last year.
Chris Johnson