Esempio n. 1
0
 def parse_any(source,parser=None):
   '''call etree.parse, automatically handling XML strings, and
     returning more meaningful exception message when possible.
   '''
   filename = None
   if isinstance(source,str):
     if source.lstrip().startswith('<'):
       source = StringIO(source)
     else:
       filename = source
   try:
     out = etree.parse(source,parser=parser)
   except etree.XMLSyntaxError,e:
     if filename is not None:
       e.args = (filename+': '+e.args[0],)
     raise
Esempio n. 2
0
 def parse_any(source, parser=None):
     """call etree.parse, automatically handling XML strings, and
   returning more meaningful exception message when possible.
 """
     filename = None
     if isinstance(source, str):
         if source.lstrip().startswith("<"):
             source = StringIO(source)
         else:
             filename = source
     try:
         out = etree.parse(source, parser=parser)
     except etree.XMLSyntaxError, e:
         if filename is not None:
             e.args = (filename + ": " + e.args[0],)
         raise
Esempio n. 3
0
 def as_element(self,arg):
   '''return arg as lxml.etree element,
     converting from string or ElementTree element as necessary.
   '''
   if etree.iselement(arg):
     return arg
   if isinstance(arg,str):
     if arg.lstrip().startswith('<'):
       source = StringIO(arg)
     else:
       # just pass filename through
       source = arg
   else:
     # assume original ElementTree object
     if simple_etree.iselement(arg):
       arg = simple_etree.ElementTree(arg)
     source = StringIO()
     arg.write(source)
     source.seek(0)
   parser = etree.XMLParser(ns_clean=True,remove_blank_text=True)
   return etree.parse(source,parser).getroot()
Esempio n. 4
0
 def as_element(self, arg):
     """return arg as lxml.etree element,
   converting from string or ElementTree element as necessary.
 """
     if etree.iselement(arg):
         return arg
     if isinstance(arg, str):
         if arg.lstrip().startswith("<"):
             source = StringIO(arg)
         else:
             # just pass filename through
             source = arg
     else:
         # assume original ElementTree object
         if simple_etree.iselement(arg):
             arg = simple_etree.ElementTree(arg)
         source = StringIO()
         arg.write(source)
         source.seek(0)
     parser = etree.XMLParser(ns_clean=True, remove_blank_text=True)
     return etree.parse(source, parser).getroot()