0

I am trying to parse an XML document using python and I am having a problem with the written accents, UTF-8 encoding should be enough. I have seen many questions about it but they didnt solve my problem her is my code:

def procesarXMLToHTML(archivoXML):

    try:
        f = open("generatedHTML.html", "w")
        
        #Escribimos la cabecera del HTML y sus metadatos
        writeHead(f)

        f.write('<body>\n')
        f.write('\t<h1> Presentacion de mi arbol genealogico hasta mis bisabuelos. </h1>\n')

        try:
            with open(archivoXML, 'r', 1, 'utf-8') as xml_file:
                arbol = ET.parse(xml_file) #Parsea el fichero XML
        except IOError:
            print ('No se encuentra el archivo ', archivoXML)
            exit()
            
        except ET.ParseError:
            print("Error procesando en el archivo XML = ", archivoXML)
            exit()
        
        raiz = arbol.getroot()
        
        print("\nElemento raiz = ", raiz.tag)

        if raiz.text != None:
            print("Contenido = ", raiz.text.strip('\n')) #strip() elimina los '\n' del string
        else:
            print("Contenido = ", raiz.text)
            
        print("Atributos = ", raiz.attrib)

        # Recorrido de los elementos del árbol
        for hijo in raiz.findall('.//'): # Expresión Path
            if(hijo.tag == 'persona'):
                f.write('\t\t<p> Persona: ' + hijo.attrib['nombre'] + ' ' + hijo.attrib['apellido'] + '</p>\n')

            print("\nElemento = " , hijo.tag)
            if hijo.text != None:
                print("Contenido = ", hijo.text.strip('\n')) #strip() elimina los '\n' del string
            else:
                print("Contenido = ", hijo.text)    
            print("Atributos = ", hijo.attrib)

        f.write('</body>\n')
        f.write('</html>\n')

    finally:
        f.close()

Ignore the prints and many ather things, I think the problem comes in that line:

with open(archivoXML, 'r', 1, 'utf-8') as xml_file:
                arbol = ET.parse(xml_file) #Parsea el fichero XML

HTML file looks like that and written accents are not shown right:

<!DOCTYPE html>
<html lang="es">
<head>
    <meta charset="UTF-8" />
    <meta name="keywords" content = "Arbol genealogico, Familia"/>
    <meta name="author" content = "Diego Glez. Su�rez"/>
    <title> Arbol Genealogico </title>
</head>

<body>
    <h1> Presentacion de mi arbol genealogico hasta mis bisabuelos. </h1>
        <p> Persona: Diego Gonz�lez Su�rez</p>
        <p> Persona: Mar�a �ngeles Su�rez Fern�ndez</p>
        <p> Persona: �ngeles Fern�ndez Prado</p>
        <p> Persona: Adela Prado Prado</p>
        <p> Persona: Belarmino Fern�ndez</p>
        <p> Persona: Jos� Ram�n Su�rez Mu�iz</p>
        <p> Persona: Mercedes Mu�iz Casero</p>
        <p> Persona: Felix Su�rez</p>
        <p> Persona: Juan Manuel Gonz�lez Garc�a</p>
        <p> Persona: Mar�a Teresa Garc�a Garc�a</p>
        <p> Persona: Oliva Garc�a Garc�a</p>
        <p> Persona: Jos� Garc�a Men�ndez</p>
        <p> Persona: Jos� Manuel Gonz�lez Fern�ndez</p>
        <p> Persona: Visitaci�n Fern�ndez Fern�ndez</p>
        <p> Persona: Manuel Gonz�lez</p>
</body>
</html>

1 Answer 1

1

Thats solved: When I create the output file it was necesary to specify the encoding.

f = open('generatedHTML.html', 'w', 1, 'UTF-8')
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.