Advertisement
Fhernd

parsear-xml-namespaces.py

Jul 28th, 2018
2,578
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.64 KB | None | 0 0
  1. from xml.etree.ElementTree import parse
  2.  
  3.  
  4. class XMLNamespace:
  5.     def __init__(self, **kwargs):
  6.         self.namespaces = {}
  7.  
  8.         for nombre, uri in kwargs.items():
  9.             self.registrar(nombre, uri)
  10.  
  11.     def registrar(self, nombre, uri):
  12.         self.namespaces[nombre] = '{' + uri + '}'
  13.  
  14.     def __call__(self, ruta):
  15.         return ruta.format_map(self.namespaces)
  16.  
  17.  
  18. documento = parse('documento_web.xml')
  19. ns = XMLNamespace(html='http://www.w3.org/1999/xhtml')
  20.  
  21. elemento = documento.find(ns('content/{html}html'))
  22. print(elemento)
  23.  
  24. texto = documento.findtext(ns('content/{html}html/{html}head/{html}title'))
  25. print(texto)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement