or you can use the approach def devicenodestring node id  node description  tag list  split multiline false for tag name  tag text in tag list xml escape tag saxutils escape tag name if split multiline xml escape text splitmultilinetonodes tag text  tag name else xml escape text saxutils escape tag text result append s  s s also  with this approach  splitmultilinetonodes should be just an inner function or private as no one needs to use it directly and can accept xml escape tag 