finally i found something and i am sorry for the time because i read something about Regular Expressions which says we cannot use RE with Maliformed Languages like XML or HTML, they hardly says that we cannot use these two things together, so i decided to do it by using DOM packages or xml parser packsges, so now let's get started:-
i created a code for you which you should firstly do some changes to it and after that use it, and i hardly suggest you to firstly use this program with some examples to learn using it first, i am not saying my code is invalid, but you said it's a large amount of files so i don't want to spend all of them wrongly, just first test it to learn how to use it easily.
Some Notes:-
1 - TagIndex
es is the index of tag name, which sometimes there are two elements with same name so use it when you it happens, it came from ** *.getElementsByTagName(...)[tagIndex]
.
2 - Firstly test it on some examples to learn using it, also you can dont do it but i don't want to loose all of your data because of some small errors, also don't scare i dont say my code has errors and you can read it yourself but this warning is because of loosing your data.
3 - Don't forget to set the containing folder.
4 - I wanted to add a future for adding elements after some specified elements or before them, but i didn't because i thought there's no need to do that, and although i created a class to do it if yourself wants.
5 - Write your managing codes in the final for loop in the specified position.
Code
import os, xml.dom.minidom as dom
from enum import Enum
#-----------------------definePath
containingFolder ="pathToContainingFolder"
files = os.listdir(containingFolder)
#if you want to add before and after specific elements
#then add this future to adding method
class addingPlace():
class types(Enum):
Parent = 0
Above = 1
Below = 2
def __init__(self, TagName, PlaceType):
self.TagName = TagName
self.PlaceType = PlaceType
def getElement(parser, tagIndex=0):
return parser.getElementsByTagName(self.TagName)[tagIndex];
#---------------------delete element
def deleteElement(selfTag, parser, tagIndex=0):
global s;
try:
s = parser.getElementsByTagName(selfTag)[tagIndex];
except:
print("Error in line 25 (tag name or tag index is invalid)")
return;
p = s.parentNode;
try:
p.removeChild(s);
except:
print("Error in line 27 (parent has no specified child)")
#---------------------add element
def addElement(tagName, parentTagName, parser, elementText=None, parentTagIndex=0):
element = dom.Element(tagName)
if(elementText is not None):
txt = dom.Text()
txt.data = elementText
element.childNodes.append(txt)
try:
parentElement = parser.getElementsByTagName(parentTagName)[parentTagIndex]
parentElement.childNodes.append(element)
except:
print("Error in line 41 (parent tag name or tag index is invalid)")
#-------------------tranfer element to specified parent
def transferElement(tagName, parentTagName, parser, tagIndex=0, parentTagIndex=0):
try:
deleting = parser.getElementsByTagName(tagName)[tagIndex];
except:
print("Error in line 47 (tag name or tag index is invalid)")
return;
element = deleting.cloneNode(True)
deleting.parentNode.removeChild(deleting)
try:
parentElement = parser.getElementsByTagName(parentTagName)[parentTagIndex]
except:
print("Error in line 53 (parent tag name or tag index is invalid)")
parentElement.childNodes.append(element)
#----------------------usage location
for f in files:
with open(os.path.join(containingFolder, f), 'r+') as fl:
fileText = fl.read()
xmlParsed = dom.parseString(fileText) #use this as parser
root = xmlParsed.documentElement.nodeName #use this as root element
#there you can use adding and deleting and trans.. methods
# this is an example:-
#addElement("C_Type",root,xmlParsed,elementText="ASCI")
formattedText = xmlParsed.toxml()
fl.seek(0);
fl.write(formattedText);
fl.truncate();