def LL2XML(LL, headings_tuple=(), root_element="rows", row_element="row", headings_attrs=(), root_attr="", row_attr="", skip_fields=[], cleanitems="yes", xml_declared = "yes"): # arguments: # # listinlist -> # root_element -> string for LL2XML output root; for whole ASV if used. If "None", not used. # row_element -> string for ASV row or list[list] row. If "None", not used. # headings_attrs -> list of strings for ASV row or list[list] heading attributes # root_attr -> string for any root element attributes # row_attr -> string for any row element attributes # skip_fields -> a list of sequence values identifying fields to ignore # -> if using field values as field attributes skip those fields for output evaluation # -> defaults to [] # cleanitems -> cleanString called for items within field list # -> headings still cleaned # -> if nesting LL2XML output within another ASV use "no". # -> defaults to "yes" # xml_declared -> defines whether XML header record is placed first in output string # -> defaults to "yes" # # see utils\asv_example.py for examples if headings_tuple == "table": td_list = [] for item in LL[0]: td_list.append("td") headings_tuple = tuple(td_list) root_element = "table" row_element = "tr" xml_declared = "no" root_element = cleanString(root_element, "tag") row_element = cleanString(row_element, "tag") if headings_tuple == (): # dwh -> will cause UserList.py split error from ASV object headings = [cleanString(s,"tag") for s in LL[0]] #LL = LL[1:] # remove now redundant heading row del LL[0] # (dwh replacing above line for ASV error.) else: headings = [cleanString(s,"tag") for s in headings_tuple] if headings_attrs == (): attributes = None else: attributes = headings_attrs # Sublists all of the same length? if ['!' for sublist in LL if len(sublist) != len(LL[0])]: raise Error("Length Error - Sublists") #check headings heading_num = len(headings) if heading_num != len(LL[0]): raise Error("Heading Error - heading/sublist missmatch", heading_num, len(LL[0])) for item in headings: if not cleanString(item,"heading"): raise Error("Heading Error - Empty Item") else: pass # Do the conversion xml = "" if xml_declared == "yes": xml_declaration = '\n' else: xml_declaration = "" bits = [] add_bit = bits.append add_bit(xml_declaration) if root_element != "none": print add_bit('<') #here add_bit(root_element) if root_attr != "": add_bit(" " + root_attr) add_bit('>') for sublist in LL: add_bit("\n <") #here add_bit(row_element) if row_attr != "": add_bit(" " + row_attr) add_bit(">\n") i = 0 for item in sublist: if skip_fields.count(i) == 0: tag = headings[i] if cleanitems == "yes": item = cleanString(item, "item") add_bit(" <") #here add_bit(tag) if (attributes != None and attributes[i] != ""): add_bit(" " + attributes[i]) add_bit(">") add_bit(item) add_bit("\n") i = i+1 add_bit(" ") if root_element != "none": add_bit("\n") xml = "".join(bits) return xml