Xml creation using ElementTree

jechowiem

New Member
I am new to python. I want to create a xml tree with one parent, several childs and several subchilds. I've stored child tags are in list 'TAG' and Subchild tags are in list 'SUB' And i have came up with following code but i am not able to achieve the desired result !\[code\]def make_xml(tag,sub):'''Takes in two lists and Returns a XML object.The first list has to contain all the tag objectsThe Second list has to contain child data's'''from xml.etree.ElementTree import Element, SubElement, Comment, tostringtop = Element("Grand Parent")comment = Comment('This is the ccode parse tree')top.append(comment)i=0try: for ee in tag: child = SubElement(top, 'Tag'+str(i)) child.text = str(tag).encode('utf-8',errors = 'ignore') subchild = SubElement(child, 'Content'+str(i)) subchild.text = str(sub).encode('utf-8',errors = 'ignore') i = i+1;except UnicodeDecodeError: print 'oops'return top\[/code\]EDIT:I have two lists like these:TAG = ['HAPPY','GO','LUCKY']SUB = ['ED','EDD','EDDY']What i want is:\[code\]<G_parent> <parent1> HAPPY <child1> ED <\child1> <\parent1> <parent2> GO <child2> EDD <\child2> <\parent2> <parent3> LUCKY <child3> EDDY <\child3 <\parent3><\G_parent>\[/code\]The actual list has many more contents than this. I want to achieve using a for loop or so.\[code\]EDIT:\[/code\]OOP's. My bad ! The code works as expected when i pass the example list. But in my real application the list is long. The list contains text fragments extracted from a pdf file. Somewhere in that text i get UnicodeDecodeError(reason: pdf extracted text messy. Proof: 'oops' get printed once ) and the returned xml object is incomplete.So I need to figure out a way that even on UnicodeDecodeErrors my complete list is parsed. Is that possible ! I'm using .decode('utf-8',errors='ignore') even then the parsing does not complete !
 
Back
Top