Advertisement
Guest User

Untitled

a guest
Apr 18th, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.06 KB | None | 0 0
  1. import xml.etree.cElementTree as ET
  2.  
  3.  
  4. # def traverse_xmltree(root):
  5. # # preorder
  6. # # child_traversals = [traverse_xmltree(child) for child in root]
  7. # child_traversals = list()
  8. # for child in root:
  9. # child_traversals.extend(traverse_xmltree(child))
  10. # pathed_child_traversals = [root.tag + "\\" + elem for elem in child_traversals]
  11. # # for elem in child_traversals:
  12. # # elem = root.tag + "\\" + elem
  13. # output = list()
  14. # output.append(root.tag)
  15. # output.extend(pathed_child_traversals)
  16. # return output
  17.  
  18. def traverse_xmltree(root):
  19. # preorder
  20. # child_traversals = [traverse_xmltree(child) for child in root]
  21. child_traversals = list()
  22. for child in root:
  23. if child.text:
  24. textline = '>>' + child.tag + '"' + child.text + '"'
  25. child_traversals.append( (textline, True) )
  26. for child in root:
  27. child_traversals.extend(traverse_xmltree(child))
  28. # pathed_child_traversals = [root.tag + "\\" + elem for elem in child_traversals]
  29. pathed_child_traversals = list()
  30. for elem in child_traversals:
  31. if not elem[1]:
  32. pathed_child_traversals.append( (root.tag + "\\" + elem[0], False) )
  33. else:
  34. pathed_child_traversals.append(elem)
  35. output = list()
  36.  
  37. # the tuple format is: (node.tag, nodeIsText).
  38. # This helps us at the very end when we need to add >> or \\ in the front.
  39. # if root.text:
  40. # output.append( (root.tag, True) )
  41. # else:
  42. # output.append( (root.tag, False) )
  43.  
  44. output.append( (root.tag, False) )
  45. output.extend(pathed_child_traversals)
  46. return output
  47.  
  48.  
  49. if __name__ == "__main__":
  50. # using actual recursion
  51. filepath = "../cleanfile.xml"
  52. tree = ET.parse(filepath)
  53. root = tree.getroot()
  54.  
  55. log = traverse_xmltree(root)
  56.  
  57. # log2 = [elem[0] for elem in log]
  58. log2 = list()
  59. for elem in log:
  60. if not elem[1]:
  61. log2.append("\\\\" + elem[0])
  62. else:
  63. log2.append(elem[0])
  64.  
  65. with open('Pathlog_jan_2.txt', 'w') as pathlog:
  66. pathlog.write("\n".join(log2))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement