davegimo

Untitled

Dec 11th, 2020 (edited)
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.84 KB | None | 0 0
  1. import xml.etree.ElementTree as ET
  2.  
  3.  
  4. def renameTag(tree, asIs, toBe):
  5.     for elem in tree.findall(".//"):
  6.         if asIs in elem.tag:
  7.             elem.tag = elem.tag.replace(asIs, toBe)
  8.             print("fatto")
  9.             print(elem.tag)
  10.  
  11.  
  12. def replaceText(tree, tag, text, toBe):
  13.     for elem in tree.findall(".//"):
  14.         if tag in elem.tag:
  15.             if text in elem.text:
  16.                 print(elem.text)
  17.                 elem.text = toBe
  18.  
  19. def deleteElement(root, tag):
  20.     b = list(root)
  21.     for e in b:
  22.         for x in e:
  23.             if tag in x.tag:
  24.                 e.remove(x)
  25.                 print(tag + "-tag removed")
  26.  
  27. def deleteDependency(root, tag):
  28.     b = list(root)
  29.     for e in b:
  30.         for dep in e:
  31.             if "dependency" in dep.tag:
  32.                 for x in dep:
  33.                     if tag in x.text:
  34.                         e.remove(dep)
  35.                         print(tag + "-dep removed")
  36.  
  37. #print(ET.tostring(root, encoding='utf8').decode('utf8'))
  38.  
  39.  
  40. #https://www.datacamp.com/community/tutorials/python-xml-elementtree
  41. tree = ET.parse("pom.xml")
  42. root = tree.getroot()
  43.  
  44. toBe = "fatttooooooo"
  45. asIs = "ReplaceMeWritePassthrough"
  46. tag = "artifactId"
  47.  
  48. renameTag(root, asIs,toBe)
  49. replaceText(root,tag, asIs, toBe)
  50.  
  51. renameTag(root, "CallToReadOnlyEndpoint", "CallGisMicroServicesEndpoint")
  52. replaceText(root,tag, "CallToReadOnlyEndpoint", "CallGisMicroServicesEndpoint")
  53.  
  54. renameTag(root, "ReadOnlyEndpoint", "GisMicroServicesEndpoint")
  55. replaceText(root,tag, "ReadOnlyEndpoint", "GisMicroServicesEndpoint")
  56.  
  57. deleteElement(root, "ReplaceMeReadOnly")
  58. deleteDependency(root, "ReplaceMeReadOnly")
  59.  
  60. deleteElement(root, "CallToWriteEndpoint")
  61. deleteDependency(root, "CallToWriteEndpoint")
  62.  
  63. deleteElement(root, "WriteEndpoint")
  64. deleteDependency(root, "WriteEndpoint")
  65.  
  66.  
  67.  
  68. tree.write("test.xml")
  69.  
Add Comment
Please, Sign In to add comment