|
We will start the same way that we did in writing XML files. Load the assembly and then create a xmlDocument
object.
Code:
--Load the xml assembly
dotNet.loadAssembly "system.xml"
--Create an xml document object.
xmlDoc=dotNetObject "system.xml.xmlDocument"
First we will set the path to the XML file that we wrote out in the previous tutorial and store it in
xmlPath. Next I'm just doing some error checking to confirm that the file does exist.
If you do a "showMethods xmlDoc" one of the methods is .load. This method will load an XML doc in several
different ways. The one that we need is loading from a file.
Every XML file has to have one root element, you can access that element using the .documentElement property.
We will set a variable for that called docEle. I usually confirm that the documentElement exists and then I test
the name property of docEle. The name property of the documentElement will return the name that we gave it. In
this case it should return "Root", I also test this to make sure that I have the right xml file loaded. Usually
I will use a more descriptive documentElement name. In the case of penHelper I use the name penObjects since
all of my penObjects can use the same format.
Once the documentElement is confirmed I am using "showProperties xmlEle" and then I'm doing the same to
to of it's properties. '.attributes' and '.childNodes'. '.attributes' will return a dotNet object that
contains all the attributes on a given element, the object if printed will look like this..
Listener:
dotNetObject:System.Xml.XmlAttributeCollection
and has all of it's own properties and methods. We will
use that later on. '.childNodes' returns an array of child elements of documentElement, we will use this
property to recurse down the hierarchy to collect all the elements and their contents.
Code:
--Open the file in Max to see the file.
xmlPath=((getDir #scripts)+"\\test.xml")
--If the file exists then load it.
if doesFileExist xmlPath then
(
--Load the XML file.
xmlDoc.load xmlPath
--Check to make sure the xmlDoc has a root element.
docEle=xmlDoc.documentElement
--If the root element exists continue.
if docEle!=undefined and docEle.name=="Root" then
(
clearListener()
format "Element Name: %\n\n" docEle.name
format "docEle Props:\n"
showProperties docEle
format "\nAttribute Props:\n"
showProperties docEle.Attributes
format "\nChildNodes Props:\n"
showProperties docEle.ChildNodes
)
)
|