Fhernd icon

parsear-xml-namespaces.py

Fhernd | PRO | 07/28/18 03:28:18 PM UTC | 0 ⭐ | 3687 👁️ | Never ⏰ | []
Python |

640 B

|

None

|

0 👍

/

0 👎

from xml.etree.ElementTree import parse
 
 
class XMLNamespace:
    def __init__(self, **kwargs):
        self.namespaces = {}
 
        for nombre, uri in kwargs.items():
            self.registrar(nombre, uri)
 
    def registrar(self, nombre, uri):
        self.namespaces[nombre] = '{' + uri + '}'
 
    def __call__(self, ruta):
        return ruta.format_map(self.namespaces)
 
 
documento = parse('documento_web.xml')
ns = XMLNamespace(html='http://www.w3.org/1999/xhtml')
 
elemento = documento.find(ns('content/{html}html'))
print(elemento)
 
texto = documento.findtext(ns('content/{html}html/{html}head/{html}title'))
print(texto)

Comments

  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎