Remover atributo do documento XML [duplicado]
Sep 18 2020
Eu tenho um documento XML
<Mapping name="AccountBalanceInquiryResolving" areaSize="31000" module="CCS">
<Input attribute="casHeaId" ldb="AccountBalanceInquiryResolvingLDB" to="casHeaId" dataType="hr.vestigo.framework.util.vtype.VBigDecimal"/>
</Mapping>
Eu preciso excluir tudo do mapeamento de tag raiz, exceto nome e módulo para ser preciso, preciso excluir o attribute areaSize.
for filename in os.listdir(path):
if not filename.endswith('.xml'):
continue
fullname = os.path.join(path, filename)
tree = ET.parse(fullname)
root = tree.getroot()
if root.tag == 'Mapping':
mapping_name= root.get('name')
mapping_module= root.get('module')
for atr in root.attrib:
if atr != 'name' and atr !='module':
print(atr)
o resultado da impressão é bom, é areaSize. Como posso removê-lo do meu documento xml?
Respostas
1 MatovinaIgor Sep 18 2020 at 20:02
aqui está a solução
for filename in os.listdir(path):
if not filename.endswith('.xml'):
continue
fullname = os.path.join(path, filename)
tree = ET.parse(fullname)
root = tree.getroot()
if root.tag == 'Mapping':
undesired = []
mapping_name= root.get('name')
mapping_module= root.get('module')
for atr in root.attrib:
if atr != 'name' and atr !='module':
undesired.append(atr)
for i in undesired:
print i
try:
del root.attrib[i]
except KeyError:
pass
tree.write('./xmlDirectory/otput.xml')
O que significa um erro “Não é possível encontrar o símbolo” ou “Não é possível resolver o símbolo”?
Christopher Nolan uma vez se arrependeu de ter lido o 'roteiro de Pulp Fiction' de Quentin Tarantino