Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import xml.etree.ElementTree as ET
- def renameTag(tree, asIs, toBe):
- for elem in tree.findall(".//"):
- if asIs in elem.tag:
- elem.tag = elem.tag.replace(asIs, toBe)
- print("fatto")
- print(elem.tag)
- def replaceText(tree, tag, text, toBe):
- for elem in tree.findall(".//"):
- if tag in elem.tag:
- if text in elem.text:
- print(elem.text)
- elem.text = toBe
- def deleteElement(root, tag):
- b = list(root)
- for e in b:
- for x in e:
- if tag in x.tag:
- e.remove(x)
- print(tag + "-tag removed")
- def deleteDependency(root, tag):
- b = list(root)
- for e in b:
- for dep in e:
- if "dependency" in dep.tag:
- for x in dep:
- if tag in x.text:
- e.remove(dep)
- print(tag + "-dep removed")
- #print(ET.tostring(root, encoding='utf8').decode('utf8'))
- #https://www.datacamp.com/community/tutorials/python-xml-elementtree
- tree = ET.parse("pom.xml")
- root = tree.getroot()
- toBe = "fatttooooooo"
- asIs = "ReplaceMeWritePassthrough"
- tag = "artifactId"
- renameTag(root, asIs,toBe)
- replaceText(root,tag, asIs, toBe)
- renameTag(root, "CallToReadOnlyEndpoint", "CallGisMicroServicesEndpoint")
- replaceText(root,tag, "CallToReadOnlyEndpoint", "CallGisMicroServicesEndpoint")
- renameTag(root, "ReadOnlyEndpoint", "GisMicroServicesEndpoint")
- replaceText(root,tag, "ReadOnlyEndpoint", "GisMicroServicesEndpoint")
- deleteElement(root, "ReplaceMeReadOnly")
- deleteDependency(root, "ReplaceMeReadOnly")
- deleteElement(root, "CallToWriteEndpoint")
- deleteDependency(root, "CallToWriteEndpoint")
- deleteElement(root, "WriteEndpoint")
- deleteDependency(root, "WriteEndpoint")
- tree.write("test.xml")
Add Comment
Please, Sign In to add comment