# defining _contains_ is not necessary, but improves speed Return iter(set( k.tag for k in self.elem) | If len(subelems) > 1 or key in self.list_tags: Here's the code: from collections import Mappingĭef _init_(self, elem, attr_prefix = list_tags = ()): Which leads me to the approach I found best for this case: parsing the xml using lxml, and then wrapping the etree nodes (lazily), providing them with a dict-like interface. The slow part in most of the other answers is the second pass: traversing the etree structure (usually in python-land), converting it to json. Approachįor this, it is important to note that parsing an xml to etree using lxml is super fast. My answer addresses the specific (and somewhat common) case where you don't really need to convert the entire xml to json, but what you need is to traverse/access specific parts of the xml, and you need it to be fast, and simple (using json/dict-like operations). Return json.dumps(self.toDict(), indent=indent) Return elements that match the given xpath. Use this to set or reset the contents of the XmlDocument. :param: filename Set this argument if you want to parse from a file. :param: xml Set this argument if you want to parse from a string. Initialize later XmlDocument will be initialized with "". You can do it later with the 'set' method. You don't have to initialize the XmlDocument during instantiation There are two ways to initialize the XmlDocument contents: cleaner access to some common lxml.etree functionsĭef _init_(self, xml = '', filename=None): :param: element a normal etree.Element instance I use the following class in my projects. You could use xmltodict but you will have to pay the penalty of parsing the XML again. When I do anything with XML in python I almost always use the lxml package.
0 Comments
Leave a Reply. |