Created
July 28, 2018 15:28
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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