Skip to content

Instantly share code, notes, and snippets.

@Fhernd
Created July 28, 2018 15:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Fhernd/cc6873c3d41b3fca7cb517275a6f6ed6 to your computer and use it in GitHub Desktop.
Save Fhernd/cc6873c3d41b3fca7cb517275a6f6ed6 to your computer and use it in GitHub Desktop.
Parsear un documento XML teniendo en cuenta sus nombres de espacio (namespaces). OrtizOL.
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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment