[Closed] Learning system.xml – translating this book.xml example
I am teaching myself dotNet in order to read and write xml. I only have time now and then to poke around and familiarize myself with everything.I bought a Linda.com DVD on XML and it all makes perfect sense. I’ve downloaded a few readymade parsers and function libraries (from TAO and others) but I’d like to learn it from the ground up instead of jumping in the deep end right away.
So in the shallow end, I am trying to translate this javascript example:
http://www.w3schools.com/dom/tryit.asp?filename=try_dom_list_loop
<html>
<head>
<script type="text/javascript" src="loadxmldoc.js"></script>
</head>
<body>
<script type="text/javascript">
xmlDoc=loadXMLDoc("books.xml");
x=xmlDoc.getElementsByTagName("author");
for (i=0;i<x.length;i++)
{
document.write(x[i].childNodes[0].nodeValue);
document.write("<br />");
}
</script>
</body>
</html>
xDoc comes up undefined, then getElementsByTagName fails…
(
clearListener()
DotNet.LoadAssembly "System.Xml"
dom = dotNetObject "System.Xml.XmlDocument"
xDoc = dom.load "c:\ est\\books.xml"
x = (xDoc.GetElementsByTagName "title")
for i = 0 to x.count do
(
print (x[i].childNodes[0].nodeValue)
print "
"
)
)
Paul Neal to the rescue…
From reading his tutorial here.
I’m happily experimenting…
(
clearListener()
dotNet.loadAssembly "System.Xml"
xmlDoc = dotNetObject "System.Xml.XmlDocument"
thePath = "c:\ est\\books.xml"
xmlDoc.load thePath
docEle=xmlDoc.documentElement
fn recurseXml ele=
(
for i = 0 to ele.childNodes.count-1 do
(
--Loop through all the attributes in an element.
for ii = 0 to ele.ChildNodes.itemOf[i].attributes.count-1 do
(
--First method
--Get the name of the attribute
attribName=ele.ChildNodes.itemOf[i].attributes.itemOf[ii].name
--Get the value of the attribute as a string.
attribVal=ele.ChildNodes.itemOf[i].attributes.itemOf[ii].value
--Second method.
--You can also get the attribute by name and then get the value from it.
attrib=ele.ChildNodes.itemOf[i].attributes.getNamedItem attribName
--Format the result to the listener
format "Book ID: %
" attrib.value
for iii = 0 to ele.ChildNodes.itemOf[i].ChildNodes.count-1 do
(
nodeName = ele.ChildNodes.itemOf[i].ChildNodes.itemOf[iii].name
nodeText = ele.ChildNodes.itemOf[i].ChildNodes.itemOf[iii].innerText
format "%: %
" nodeName nodeText
)
format "
"
)
)
)
format "Element Name: %
" docEle.name
format "docEle Props:
"
showProperties docEle
format "
Attribute Props:
"
showProperties docEle.Attributes
format "
ChildNodes Props:
"
showProperties docEle.ChildNodes
format "
Children of First Node: %
" (recurseXml docEle)
)
If you’re looking to learn the .NET XML classes, you might want to do this in a C#/C++/VB.NET + visual studio environment rather than maxscript. Development in visual studio is so much easier and faster, especially when exploring possibilities of classes.
For example, you can debug really easily, and intellisense (code completion, error highlighting, hints etc) in C# in particular is very powerful. And once you’ve got a grasp of the classes, making the translation to maxscript is quite easy.
Just a thought
Thanks Paul,
Your tuts got me a lot further. What I really need is to take a leave of absence and pay to be an apprentice under someone like you. It would accelerate my learning quite a bit.
Pjanssen,
Over the years, I’ve played with the light version of Visual Studio, I have a couple of C# books (and VB), I’ve gone through the basics and made a calculator or two…
A great book opportunity for one of you Gurus (or for Gnomon) would be a thorough dotNet/MXS instructional. Geared toward pipeline, work-flow, asset management etc. Instead of rigging. There are plenty of rigging tutorials for now. (no offense Paul!)
I realize a lot of this type of development may be under NDA or proprietary.