[EMAIL PROTECTED] wrote: > My request for advice is this: instead of creating a second XML > document that represents the output, would it be possible to expand the > input XML document as needed? I was thinking that the program could > iterate through all the elements. As it is iterating, it would check > for the createAnotherWhenCondition attribute. If encountered and if > the condition were true, the program would: > - make a copy of the parent element (perhaps with copy.copy) > - use the insert method to insert the just-created copy > Where I'm struggling is figuring out what the index argument should > be in the insert method.
I recommend not using the insert method; instead, build a new list of elements and set the parent to the new list. Another problem is that you have to have the parent node around, since children don't indicate their parents. Using getiterator won't work. I recommend a recursive function instead. Something like this should do it: def expand_children(parent): if len(parent) == 0: return newchildren = [] for child in parent: expand_children(child) # recursively process child nodes if <you're supposed to create another child>: <remove createAnotherWhenCondition attr> newchildren.append(child) # or copy.copy(child) newchildren.append(child) parent[:] = newchildren # slice assign So, basically, for each node, you build a list of children in parallel, making duplicates as necessary, then use slice assignment to set the parent's children to be the new list. No more mucking around with indexes. Carl Banks -- http://mail.python.org/mailman/listinfo/python-list