0
Hello! I am creating a program in Python where the output of the program will be an XML script in the ABAP language (for a SAP specific transport), but when I export the output to XML it does not export identada, that is, everything in the same line.
How does it come out:
<lista><usuario><user name="user">admin</user><senha name="senha">123456</senha><nome name="nome">Fulano de Tal</nome></usuario></lista>
How I want it to go:
<lista>
   <usuario>
       <user name="user">admin</user>
       <senha name="senha">123456</senha>
       <nome name="nome">Fulano de Tal</nome>
   </usuario>
</lista>
The code I’m using:
import xml.etree.cElementTree as ET
class XML(object):
 def __init__(self, usuario, senha, nome):
    self.usuario = usuario
    self.senha = senha
    self.nome = nome
 def gerar_xml(self):    
    lista = ET.Element("lista")
    no_usuario = ET.SubElement(lista, "usuario")
    ET.SubElement(no_usuario, "user", name="user").text = self.usuario
    ET.SubElement(no_usuario, "senha", name="senha").text = self.senha
    ET.SubElement(no_usuario, "nome", name="nome").text = self.nome                
    arquivo = ET.ElementTree(lista)
    arquivo.write("meu_xml.xml")
def main():
  xml = XML("admin", "123456", "Fulano de Tal")
  xml.gerar_xml()    
main()
It is pq in case, the user will have q take the contents of this generated XML and add in the Final script manually. And in the final script it’s idented, so to avoid confusion I chose to leave it that way too. I used the LXML API with the pretty_print command and it worked right! Thank you!
– Lucas Cruz