Xml traversal

This is the place for queries that don't fit in any of the other categories.

Xml traversal

Postby bajaj » Fri May 31, 2013 2:24 am

Hi ,
I am trying to visit every element on an xml through recursion
Code: Select all
tree=ElementTree.parse(anyxml.xml)
root=tree.getroot()
   t=recur(root)  #Passing the xml root elemnet
   print(t)

               
               
def recur(e):
   
   d={}
   d['tag']=e.tag
   d['children']=[]
   for ch in e.iter():
                           #print(ch)

                          d['children']=(recur(ch))





   return(child_dict)
 


I am getting an runtime error: max recursion depth. I am not sure what's going wrong. If I am print the vale of ch is working fine.
bajaj
 
Posts: 2
Joined: Wed May 29, 2013 6:41 pm

Re: Xml traversal

Postby Mekire » Fri May 31, 2013 4:53 am

Well assuming your code is doing what you want, you can change the max recursion depth. The default seems to be 1000.

You can change it as follows:
Code: Select all
import sys
sys.setrecursionlimit(desired_limit)

-Mek
User avatar
Mekire
 
Posts: 828
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan


Return to General Coding Help

Who is online

Users browsing this forum: No registered users and 3 guests