2222
2323import inspect
2424
25- try :
26- from xml .etree import cElementTree as ElementTree
27- except ImportError :
28- try :
29- import cElementTree as ElementTree
30- except ImportError :
31- try :
32- from xml .etree import ElementTree
33- except ImportError :
34- from elementtree import ElementTree
25+ import lxml .etree as ElementTree
3526
3627try :
3728 from xml .dom .minidom import parseString as xmlString
@@ -295,12 +286,12 @@ def _harvest_tree(self, tree, version=1):
295286 if tree .text :
296287 self .text = tree .text
297288
298- def _to_tree (self , version = 1 , encoding = None ):
289+ def _to_tree (self , version = 1 ):
299290 new_tree = ElementTree .Element (_get_qname (self , version ))
300- self ._attach_members (new_tree , version , encoding )
291+ self ._attach_members (new_tree , version )
301292 return new_tree
302293
303- def _attach_members (self , tree , version = 1 , encoding = None ):
294+ def _attach_members (self , tree , version = 1 ):
304295 """Convert members to XML elements/attributes and add them to the tree.
305296
306297 Args:
@@ -314,7 +305,7 @@ def _attach_members(self, tree, version=1, encoding=None):
314305 encoding: str (optional)
315306 """
316307 qname , elements , attributes = self .__class__ ._get_rules (version )
317- encoding = encoding or STRING_ENCODING
308+ encoding = STRING_ENCODING
318309 # Add the expected elements and attributes to the tree.
319310 if elements :
320311 for tag , element_def in elements .items ():
@@ -348,7 +339,7 @@ def _attach_members(self, tree, version=1, encoding=None):
348339 def to_string (self , version = 1 , encoding = None , pretty_print = None ):
349340 """Converts this object to XML."""
350341
351- tree_string = ElementTree .tostring (self ._to_tree (version , encoding ))
342+ tree_string = ElementTree .tostring (self ._to_tree (version ))
352343
353344 if pretty_print and xmlString is not None :
354345 return xmlString (tree_string ).toprettyxml ()
@@ -362,7 +353,7 @@ def __str__(self):
362353
363354 def _become_child (self , tree , version = 1 ):
364355 """Adds a child element to tree with the XML data in self."""
365- new_child = ElementTree .Element ('' )
356+ new_child = ElementTree .Element (_get_qname ( self , version ) )
366357 tree .append (new_child )
367358 new_child .tag = _get_qname (self , version )
368359 self ._attach_members (new_child , version )
@@ -497,11 +488,11 @@ def _qname_matches(tag, namespace, qname):
497488 and member_namespace is None ))
498489
499490
500- def parse (xml_string , target_class = None , version = 1 , encoding = None ):
491+ def parse (xml_string , target_class = None , version = 1 ):
501492 """Parses the XML string according to the rules for the target_class.
502493
503494 Args:
504- xml_string: str or unicode
495+ xml_string: bytes
505496 target_class: XmlElement or a subclass. If None is specified, the
506497 XmlElement class is used.
507498 version: int (optional) The version of the schema which should be used when
@@ -511,11 +502,8 @@ def parse(xml_string, target_class=None, version=1, encoding=None):
511502 """
512503 if target_class is None :
513504 target_class = XmlElement
514- if isinstance (xml_string , str ):
515- if encoding is None :
516- xml_string = xml_string .encode (STRING_ENCODING )
517- else :
518- xml_string = xml_string .encode (encoding )
505+ if not isinstance (xml_string , bytes ):
506+ raise Exception ("This function only accepts bytes" )
519507 tree = ElementTree .fromstring (xml_string )
520508 return _xml_element_from_tree (tree , target_class , version )
521509
0 commit comments