May-30-2023, 12:56 PM
(This post was last modified: May-30-2023, 04:37 PM by snippsat.
Edit Reason: Added code tag
)
Hello everybody
I would like to ask for some help.
I face a problem when trying to edit an xml file with python and lxml.etree
When trying to edit the data of the numeroCarteira tag, it does not edit properly.
Can anyone help me?
this is the code
I would like to ask for some help.
I face a problem when trying to edit an xml file with python and lxml.etree
When trying to edit the data of the numeroCarteira tag, it does not edit properly.
Can anyone help me?
this is the code
import lxml.etree as ET # Carregar o arquivo XML tree = ET.parse('exemplo.xml') root = tree.getroot() # Definir o namespace namespace = {'ns0': 'http://www.ans.gov.br/padroes/tiss/schemas'} # Navegar e fazer alterações no XML for elemento in root.xpath('//ns0:elemento', namespaces=namespace): numero_carteira = elemento.xpath('.//ns0:numeroCarteira', namespaces=namespace)[0].text # Verificar o número de caracteres if len(numero_carteira) < 6: numero_carteira = '00' + numero_carteira # Atualizar o valor do elemento elemento.xpath('.//ns0:numeroCarteira', namespaces=namespace)[0].text = numero_carteira # Salvar as alterações de volta no arquivo tree.write('exemplo.xml', encoding='UTF-8', xml_declaration=True)