Re: [Maya-Python] Python´s xml.dom.minidom and Maya

131 views
Skip to first unread message

Justin Israel

unread,
Aug 24, 2012, 12:56:44 PM8/24/12
to python_in...@googlegroups.com
There is a pretty simple looking minidom example here:
http://www.postneo.com/projects/pyxml/
Shows you just about all you will need to accomplish the exporting of
the values.You will probably end up doing something very similar to
this, using a bunch of createElement() calls, and setting attributes
on them like:

###
from xml.dom.minidom import Document

doc = Document()

root = doc.createElement("nodes")
doc.appendChild(root)

node = doc.createElement("node1")
node.setAttribute("path", "|foo|bar")
root.appendChild(node)

attr = doc.createElement("attrib")
attr.setAttribute("name", "tx")
attr.setAttribute("type", "float")
val = doc.createTextNode("10.5")
attr.appendChild(val)
node.appendChild(attr)

attr = doc.createElement("attrib")
attr.setAttribute("name", "visibility")
attr.setAttribute("type", "int")
val = doc.createTextNode("0")
attr.appendChild(val)
node.appendChild(attr)

print doc.toprettyxml(indent=" ")
###

Which will create:

<?xml version="1.0" ?>
<nodes>
<node1 path="|foo|bar">
<attrib name="tx" type="float">
10.5
</attrib>
<attrib name="visibility" type="int">
0
</attrib>
</node1>
</nodes>


There are even packages our there to help you automatically serialize
python objects to xml. But honestly though, if you aren't bound to
XML, then JSON would be a whole lot better for storing data types.
XML doesn't natively support any concept of data types, which means
you have to parse them back out to the correct type yourself, based on
the xml tags, or by doing an eval on them to test the types. JSON
would simple be like this:

import json

d = {
'|foo|bar': {
'tx': 10.5,
'visibility': 0
}
}

j = json.dumps(d, indent=4)
print j

And looks like:

{
"|foo|bar": {
"visibility": 0,
"tx": 10.5
}
}





On Fri, Aug 24, 2012 at 6:03 AM, 5inch <5i...@gmx.de> wrote:
>
> Hi Folks,
>
> has anyone of you experience with
> Python´s xml.dom.minidom and Maya.
>
> I want to export some Maya Attributes (Int,String,Float)
> via Python in a XML-File.
> I didn´t find any good howTo/Tutorial on the Web which explains me all the syntax in a
> Dummie-fashion-way.
>
> Thanks for any Links/help,
>
> Cheers, 5inch
>
> --
> view archives: http://groups.google.com/group/python_inside_maya
> change your subscription settings: http://groups.google.com/group/python_inside_maya/subscribe

Jay Goodman

unread,
Aug 28, 2012, 1:45:36 AM8/28/12
to python_in...@googlegroups.com
I agree with the JSON route.  It has a lot less overhead to deal with, and you don't have to deal with everything being converted to strings. If you are stuck with xml though, I really like cElementTree because it is fast:  http://effbot.org/zone/celementtree.htm 

Justin Israel

unread,
Aug 28, 2012, 10:48:12 AM8/28/12
to python_in...@googlegroups.com, python_in...@googlegroups.com
I also hear lxml is the fastest, if you end up testing different xml libraries. 


Judah Baron

unread,
Sep 26, 2012, 7:34:47 PM9/26/12
to python_in...@googlegroups.com
It's probably a little late on this thread to be useful to you, but I'll respond in case this helps anyone else.

Personally, I'd probably switch to JSON if we didn't have a historical reliance on XML. The JSON structure if effectively identical to that of XML: they are both flexible, hierarchical data structures. As others have mentioned, JSON has built in typing, support for arrays, and is more compact than xml. 

If you must stick with XML:
My experience with minidom left me underwhelmed. It's slow.
cElement tree and lxml are quite comparable in terms of speed. For certain types of activity (parsing, specifically), cElementTree is actually faster because of the way it handles the relationships of the python objects to the C++ objects. Lxml builds out a more complex tree that allows you to parse the xml structure once it's in memory, in ways you cannot in cElementTree: get an element's parent, for instance. Also, cElement tree does NOT preserve comments, so it's not a good working format.

The nice thing about lxml, cElementTree and ElementTree, for that matter, is that they all share the same interface, so if you abstract your calls through an intermediary, you can easily switch between the three libraries. Something like this works well:

try:
import lxml.etree as ET
except:
print 'lxml NOT FOUND. Attempting to load cElementTree.'
try:
from xml.etree import cElementTree as ET
except:
print 'cElementTree NOT FOUND. Attempting to load etree.'
try:
from xml.etree import ElementTree as ET
except:
print 'NO XML MODULE FOUND'

There is quite a lot out there on the web if you care to read more about specific differences between lxml and cElementTree.

-Judah


On Wed, Sep 19, 2012 at 8:13 PM, DayDreaner <day.drea...@gmail.com> wrote:
Ya, lxml is very fast.

One Query regarding JSON and XML

Personally, i feel xml provides a nice structure, whereas Json is just a dictionary(like pickling).
What about large data and what if I want to edit the serialized file manually(for any wiered reason) and don't want to generate the file again(as it is huge).

Thanks

------
Day Dreamer
www.daydreamer3d.weebly.com
Reply all
Reply to author
Forward
0 new messages