KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
I have to handle xml documents that are big enough (up to 1GB) and parse them with python. I am using the iterparse() function (SAX style parsing). My concern is the following, imagine you have an xml like this <?xml version="1.0" encoding="UTF-8" ?> <families> <family> <name>Simpson</name> <members> <name>Homer</name> <name>Marge</name> <name>Bart</name> </members> </family> <family> <name>Griffin</name> <members> <name>Peter</name> <name>Brian</name> <name>Meg</name> </members> </family> </families> The problem is, of course to know when I am getting a family name (as Simpsons) and when I am getting the name of one of that family member (for example Homer) What I have been doing so far is to use "switches" which will tell me if I am inside a "members" tag or not, the code will look like this import xml.etree.cElementTree as ET __author__ = 'moriano' file_path = "test.xml" context = ET.iterparse(file_path, events=("start", "end")) # turn it into an iterator context = iter(context) on_members_tag = False for event, elem in context: tag = elem.tag value = elem.text if value : value = value.encode('utf-8').strip() if event == 'start' : if tag == "members" : on_members_tag = True elif tag == 'name' : if on_members_tag : print "The member of the family is %s" % value else : print "The family is %s " % value if event =
Tags (comma-separated)
Save Edits
Cancel