Python Forum
xml file editing with lxml.etree
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
xml file editing with lxml.etree
#1
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
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)
snippsat write May-30-2023, 04:36 PM:
Added code tag in your post,look at BBCode on how to use.
Reply
#2
Post sample of exemplo.xml and explain output wanted.
Reply
#3
Sorry for the delay guys.

I was able to fix my code by changing the xml navigation lines.

''' CORRIGIR CONSULTA  ''' 

import xml.etree.ElementTree as ET
import os

# Obter a lista de nomes de arquivo XML no diretório
diretorio = 'C:/Users/flavio.bueno/Projetos_Python/XML'
arquivos_xml = [arquivo for arquivo in os.listdir(diretorio) if arquivo.endswith('.xml')]


# Processar cada arquivo XML
for arquivo in arquivos_xml:
    # Carregar o arquivo XML
    caminho_arquivo = os.path.join(diretorio, arquivo)
    tree = ET.parse(caminho_arquivo)
    root = tree.getroot()

    # Definir o namespace
    namespace = {'ans': 'http://www.ans.gov.br/padroes/tiss/schemas'}    
    
    # Navegar e fazer alterações no XML
    for elemento in root.findall('.//ans:guiaConsulta', namespace):
        numero_carteira = elemento.find('.//ans:numeroCarteira', namespace)

        # Verificar o número de caracteres
        if len(numero_carteira.text) < 6:
            numero_carteira.text = '00' + numero_carteira.text
        
        elif len(numero_carteira.text) < 7:
            numero_carteira.text = '0' + numero_carteira.text  
    
    # Salvar as alterações de volta no arquivo
    tree.write(caminho_arquivo, encoding='UTF-8', xml_declaration=True)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Regarding file editing dan86 12 2,568 Jun-29-2022, 07:13 PM
Last Post: dan86
  xml.etree.ElementTree question. water 0 3,293 Oct-09-2020, 06:47 PM
Last Post: water
  xml.etree.ElementTree extract string values matthias100 2 5,000 Jul-12-2020, 06:02 PM
Last Post: snippsat
  [pykml] "AttributeError: 'lxml.etree._ElementTree' object has no attribute 'Document' Winfried 3 6,606 May-26-2020, 09:30 PM
Last Post: Winfried
  pdf file processing: how to "Enable Editing" Pavel_47 4 3,220 Dec-04-2019, 10:00 AM
Last Post: Pavel_47
  looking for sample py to read from txt file into XML using lxml import etree venkat18 3 3,007 Jun-02-2019, 04:34 AM
Last Post: venkat18
  lxml - etree/lxml need help storing variable for most inserted element goeb 0 2,568 Apr-01-2019, 03:09 AM
Last Post: goeb
  Parsing and Editing a Structured Text File norsemanGrey 1 2,438 Jul-11-2018, 09:51 PM
Last Post: Larz60+
  XML using xml.etree.ElementTree Question Ngea 1 2,752 Jan-10-2018, 08:51 PM
Last Post: Gribouillis
  Reusing same namespace name when writing back XML with xml.etree.ElementTree Ofnuts 1 9,871 Oct-21-2016, 07:25 AM
Last Post: Ofnuts

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020