Advertisement
davegimo

Untitled

Dec 14th, 2020
658
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.  
  5. def renameTag(tree, asIs, toBe):
  6.     for elem in tree.findall(".//"):
  7.         if asIs in elem.tag:
  8.             elem.tag = elem.tag.replace(asIs, toBe)
  9.             print("fatto")
  10.             print(elem.tag)
  11.  
  12.  
  13. def replaceText(tree, tag, text, toBe):
  14.     for elem in tree.findall(".//"):
  15.         if tag in elem.tag:
  16.             if text in elem.text:
  17.                 print(elem.text)
  18.                 elem.text = toBe
  19.  
  20.  
  21. def deleteElement(root, tag):
  22.     b = list(root)
  23.     for e in b:
  24.         for x in e:
  25.             if tag in x.tag:
  26.                 e.remove(x)
  27.                 print(tag + "-tag removed")
  28.  
  29.  
  30. def deleteDependency(root, tag):
  31.     b = list(root)
  32.     for e in b:
  33.         for dep in e:
  34.             if "dependency" in dep.tag:
  35.                 for x in dep:
  36.                     if tag in x.text:
  37.                         e.remove(dep)
  38.                         print(tag + "-dep removed")
  39.  
  40. #print(ET.tostring(root, encoding='utf8').decode('utf8'))
  41.  
  42.  
  43. #https://www.datacamp.com/community/tutorials/python-xml-elementtree
  44. tree = ET.parse("pom.xml")
  45. root = tree.getroot()
  46.  
  47. toBe = "fatttooooooo"
  48. asIs = "ReplaceMeWritePassthrough"
  49. tag = "artifactId"
  50.  
  51. renameTag(root, asIs,toBe)
  52. replaceText(root,tag, asIs, toBe)
  53.  
  54. renameTag(root, "CallToReadOnlyEndpoint", "CallGisMicroServicesEndpoint")
  55. replaceText(root,tag, "CallToReadOnlyEndpoint", "CallGisMicroServicesEndpoint")
  56.  
  57. renameTag(root, "ReadOnlyEndpoint", "GisMicroServicesEndpoint")
  58. replaceText(root,tag, "ReadOnlyEndpoint", "GisMicroServicesEndpoint")
  59.  
  60. deleteElement(root, "ReplaceMeReadOnly")
  61. deleteDependency(root, "ReplaceMeReadOnly")
  62.  
  63. deleteElement(root, "CallToWriteEndpoint")
  64. deleteDependency(root, "CallToWriteEndpoint")
  65.  
  66. deleteElement(root, "WriteEndpoint")
  67. deleteDependency(root, "WriteEndpoint")
  68.  
  69.  
  70.  
  71. tree.write("test.xml")
  72.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement