xml.etree disfunctions with ArcGIS10.0

1599
3
12-06-2010 12:42 PM
GabrielHuot-Vézina
New Contributor II
Hi !

I'm currently trying to parse an xml file.
Pretty simple, the xml file is a file to store custom environment parameters. The xml is made within Arc by one of my other script, with xml.etree lib. From there, everything is fine, except when I use the same lib. to parse my xml and read a node within it.
In PythonWin, my code works, in an ArcToolbox, it works only if I run the code outside arc process, wich take an awfull lot of time.
I know and I've read about the fact that some libs could not work within arc process and that we should run it outside...but it's only a function within the same lib that doesn't work...I think it's a problem ESRI should debug.
Here the error that comes up when I run my script:
<type 'exceptions.ImportError'>:No module named expat; use SimpleXMLTreeBuilder instead

Has anyone encountered the same problem or could indicate me how to bypass that?

regards,

Gab
Tags (2)
0 Kudos
3 Replies
LoganPugh
Occasional Contributor III
Are you using arcgisscripting or ArcPy? I have a script I made in 9.3 that works in 10 (still using arcgisscripting) that has no problem with the ElementTree class.
0 Kudos
GabrielHuot-Vézina
New Contributor II
Great idea thanks ! but...I tried it, doesn't work either. In fact, when you think about it the arc libs (arcsgiscripting or arcpy) doesn't have anything to do with me trying to read an xml, so it's a ok that it crash again.

I tried it with a 9.3 station and it worked...so I'm confused now.
My problem is with this function: ElementTree.parse.
Here a bit of code:

from xml.etree import ElementTree

tree = ElementTree.parse(r'C:\MyFile.xml') #Will crash here in ArcGIS 10.0 SP1
Node = tree.getiterator("MyNode")
MyNode = Node[0].text


edit
-----
I tried bypassing my problem by re-initializing all default lib path within python, in my Arc script. I emptied the list of paths, and added them all again one by one, except those from ArcGIS itself. When I runned my script in PythonWin, I got the same message, "missing module expat use SimpleXMLTreeBuilder instead".
Here, the list of all default python lib paths:
['', 'C:\\Python26', 'C:\\windows\\system32\\python26.zip', 'C:\\Python26\\ArcGIS10.0\\DLLs', 'C:\\Python26\\ArcGIS10.0\\lib', 'C:\\Python26\\ArcGIS10.0\\lib\\plat-win', 'C:\\Python26\\ArcGIS10.0\\lib\\lib-tk', 'C:\\Python26\\ArcGIS10.0\\Lib\\site-packages\\pythonwin', 'C:\\Python26\\ArcGIS10.0', 'C:\\Python26\\ArcGIS10.0\\lib\\site-packages', 'C:\\Program Files (x86)\\ArcGIS\\Desktop10.0\\bin', 'C:\\Program Files (x86)\\ArcGIS\\Desktop10.0\\arcpy', 'C:\\Program Files (x86)\\ArcGIS\\Desktop10.0\\ArcToolbox\\Scripts', 'C:\\Python26\\ArcGIS10.0\\lib\\site-packages\\win32', 'C:\\Python26\\ArcGIS10.0\\lib\\site-packages\\win32\\lib']

And here the code I used to extract those path:
>>> import os
>>> os.sys.path


Use a "for" iteration to pop them all from the list and append them afterward.

Gab
0 Kudos
GabrielHuot-Vézina
New Contributor II
Hi me again !

It works, I manage to bypass the problem.

First, it's obvious there is something missing within arc's list of python paths, like the expat module.

For those of you who have already searched a little within Python's folder on your disk, you have noticed that everything, or almost, from Python is copied within an ArcGIS10.0 folder within Python26, on you C disk.
Compare C:\Python26 with C:\Python26\ArcGIS10.0. Than, when you call a script within arc, it will search in C:\Python26\ArcGIS10.0, and the expat module seems to be bugged within it.

So, the trick is to access the pythons defaults path, with the code I posted in my last reply, empty it from every Arc's path, and reload the default one from python and force it to search within xml folders.
It gives something like this:
import os, sys
while len(os.sys.path) != 0:
      for elems in os.sys.path:
           os.sys.path.remove(elems)
os.sys.path.append(r'C:\Python26\Lib\xml') #for etree lib
os.sys.path.append(r'C:\Python26\Lib\xml\parsers') #for expat lib
os.sys.path.append(r'C:\Python26\ArcGIS10.0\DLLs') #for arc to run it, at least

from xml.etree import ElementTree
import arcpy as AP

tree = ElementTree.parse(r'C:\MyFile.xml')
Node = tree.getiterator('MyNode')
MyNode = Node[0].text

AP.AddMessage(MyNode)


Gab