algorithmic modeling for Rhino
I've been trying to get my head around XML and Python in Grasshopper.
How are you managing XML in grasshoppers python plugin?
After some investigation I decided to go with an updated ElementTree 1.3 and the Python plugin (IronPython seems to be pre 2.7).
After finally getting my setup to parse and work based on the most simple example files. I'm stuck going forward with XML namespaces.
Can't get ElementTree.register_namespace() to work, think it's due to old python version. Instead I found an old tutorial and workaround defining a variable namespace and using format().
This method worked fine until i got to the fourth nested "child" from root, which is a XLMNS in the XML file. The code below results in:
print "Find: " '{0}define'.format(namespace), "in:"
print [elem.tag for elem in root.getiterator() if elem is not root]
print "Is Object: ", root.getiterator('{0}define'.format(namespace))
for define in root.getiterator('{0}define'.format(namespace)):
print define.tag, define.attrib, define.text
try:
define
except NameError:
for define in root.getiterator('{http://schemas.mathsoft.com/worksheet50}define'):
print define.tag, define.attrib, define.text
try:
define
except NameError:
print "Didn't find " '{0}define'.format(namespace)
>>>
Find: {http://schemas.mathsoft.com/worksheet50}define in:
['{http://schemas.mathsoft.com/worksheet50}regions',
#regions being first child
'{http://schemas.mathsoft.com/worksheet50}region',
'{http://schemas.mathsoft.com/worksheet50}math',
#math being third child, which I can iterate and find
'{http://schemas.mathsoft.com/math50}define', '{http://schemas.mathsoft.com/math50}id', '{http://schemas.mathsoft.com/math50}placeholder',
#.... Long list, can't find any child deeper than math ....
]
Is Object: <generator object at 0x000000000000003F>
{http://schemas.mathsoft.com/worksheet50}math {'resultRef': '0'} None
{http://schemas.mathsoft.com/worksheet50}math {'resultRef': '2'} None
{http://schemas.mathsoft.com/worksheet50}math {'resultRef': '4'} None
Didn't find {http://schemas.mathsoft.com/worksheet50}define
Any thoughts?
Does cElementTree work better with namespaces or is it the same?
From XML file:
<worksheet ... xmlns:ml="http://schemas.mathsoft.com/math50">
<regions>
<region>
<math>
<ml:define>
<ml:id>
....
</ml:id>
</ml:define>
</math>
and so on..
Tags:
Welcome to
Grasshopper
Added by Parametric House 0 Comments 0 Likes
Added by Parametric House 0 Comments 0 Likes
Added by Parametric House 0 Comments 0 Likes
© 2024 Created by Scott Davidson. Powered by