0

I created a XML-Reader that creates a pretty stupid formated List, the script thats goint to use it needs it this way though.

import xml.etree.ElementTree as ET

PATH_IN = "<Path>\sweep.xml"

tree = ET.parse(PATH_IN)
root = tree.getroot()

Input = []
for project in root:

    for design in project:
        list_lvl2 = []
        list_lvl2.append(project.get('name'))
        list_lvl2.append(design.get('name'))
        list_lvl2.append('')

        list_lvl3 = [] 
        for param in design:
            list_lvl4 = []
            list_lvl4.append(param.get('name'))
            list_lvl5 = []
            for steps in param:
                list_lvl5.append(steps.text)
            list_lvl4.append(list_lvl5)
            list_lvl4.append(param.get('unit'))
            list_lvl3.append(list_lvl4)
        list_lvl2.append(list_lvl3)
        Input.append(list_lvl2)

Problem is I have to execute it in the Scripting Interface of a Program which uses IronPython 2.6.10920.0 on .NET 2.0.50727.5466. And it throws following error:

*Global - Messages
  [error] ImportException: No module named expat; use SimpleXMLTreeBuilder instead  In file "<Path>/sweep.py", line 36 ---- While executing script: <Path>/sweep.py
  [error] Error executing script in <Path>\sweep.py:
  [error] ImportException: No module named expat; use SimpleXMLTreeBuilder instead  In file "<Path>/sweep.py", line 36 ---- While executing script: <Path>/sweep.py
  [error] Error executing script in <Path>\sweep.py:

What I don't understand is that this script which uses the same module works fine:

import xml.etree.ElementTree as ET

path = "<Path>\projects.xml"

root = ET.Element('xml')
projects = oDesktop.GetProjectList()
for i in projects:
    project = ET.SubElement(root,'project')
    project.set('name', i)
    designs = oDesktop.SetActiveProject(i).GetTopDesignList()
    for u in designs:
        design = ET.SubElement(project,'design')
        design.set('name', u)
        vars = oDesktop.SetActiveProject(i).GetDesign(u).GetVariables()
        for z in vars:
            param = ET.SubElement(design,'param')
            param.set('name', z)
            value = ET.SubElement(param,'value')
            value.text = oDesktop.SetActiveProject(i).GetDesign(u).GetVariableValue(z)
f = open(path, "w")
tree = ET.ElementTree(root)
tree.write(f)

f.close()

oDesktop and such are just programspecific functions that fetch data. I really don't understand why the first script won't work, I'm guessing because of NET 2.0.

UPDATE: After looking up what expat is, I'm even more confused since I never use it anywhere and all elementtree functions I use should be supported whith this version of Iron Python. Still it gives me the expat error on this line tree = ET.parse(PATH_IN)

UPDATE: I tried using from elementtree import SimpleXMLTreeBuilder as ET which resulted in:

*Global - Messages
  [error] ImportException: No module named elementtree  In file "<Path>/sweep.py", line 3 ---- While executing script: <Path>/sweep.py
  [error] Error executing script in <Path>\sweep.py:
2
  • By default expat is used by ElementTree to parse the xml. Your other script is writing xml not parsing it, so there's no problem. It's still strange that you get this error, though. Maybe your IronPython install is broken? Try using another treebuilder, or even finding/making your own treebuilder using a .NET XML parser. Commented Feb 26, 2013 at 12:49
  • I don't have a elementtree folder in my iron python dictionary are there any other xml reader that don't use expat? Commented Feb 26, 2013 at 14:59

1 Answer 1

1

The basic issue here is that ElementTree uses expat for xml parsing, but expat (a C library made available to Python via a CPython wrapper) cannot be used from IronPython.

However, ElementTree can use a different tree builder driven by a different parser, e.g. the XMLReader parser in .NET. A search for "xmlreader treebuilder" yields this result which seems to both describe your problem and provide an alternative treebuilder implementation (MIT license) which uses XMLReader.

Sign up to request clarification or add additional context in comments.

2 Comments

I already found this github.com/ixc/elementtree. It seems that this libary was missing in the IronPython implementation I have to use at work. Whith it i could use SimpleXMLTreeBuilder. Still I wouldn't have figured that out without your help so thank you very much
oh and expat works in the latest Iron Python version it seems they just had an issue with 2.6 ironpython.codeplex.com/workitem/31579

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.