【问题标题】:How to convert an XML string to a dictionary?如何将 XML 字符串转换为字典?
【发布时间】:2011-01-10 00:36:55
【问题描述】:

我有一个从套接字读取 XML 文档的程序。我将 XML 文档存储在一个字符串中,我想直接将其转换为 Python 字典,这与在 Django 的 simplejson 库中完成的方式相同。

举个例子:

str ="<?xml version="1.0" ?><person><name>john</name><age>20</age></person"
dic_xml = convert_to_dic(str)

然后dic_xml 看起来像{'person' : { 'name' : 'john', 'age' : 20 } }

【问题讨论】:

  • str 有一些语法错误。试试:str ='john20'

标签: python xml json dictionary xml-deserialization


【解决方案1】:

最容易使用 Python 的 XML 解析器是 ElementTree(2.5x 及以上版本位于标准库 xml.etree.ElementTree 中)。我认为没有什么可以完全满足您的要求。使用 ElementTree 写一些东西来做你想做的事情是很简单的,但是为什么要转换为字典,为什么不直接使用 ElementTree。

【讨论】:

    【解决方案2】:

    这是一个指向ActiveState solution 的链接 - 以及代码,以防它再次消失。

    ==================================================
    xmlreader.py:
    ==================================================
    from xml.dom.minidom import parse
    
    
    class NotTextNodeError:
        pass
    
    
    def getTextFromNode(node):
        """
        scans through all children of node and gathers the
        text. if node has non-text child-nodes, then
        NotTextNodeError is raised.
        """
        t = ""
        for n in node.childNodes:
        if n.nodeType == n.TEXT_NODE:
            t += n.nodeValue
        else:
            raise NotTextNodeError
        return t
    
    
    def nodeToDic(node):
        """
        nodeToDic() scans through the children of node and makes a
        dictionary from the content.
        three cases are differentiated:
        - if the node contains no other nodes, it is a text-node
        and {nodeName:text} is merged into the dictionary.
        - if the node has the attribute "method" set to "true",
        then it's children will be appended to a list and this
        list is merged to the dictionary in the form: {nodeName:list}.
        - else, nodeToDic() will call itself recursively on
        the nodes children (merging {nodeName:nodeToDic()} to
        the dictionary).
        """
        dic = {} 
        for n in node.childNodes:
        if n.nodeType != n.ELEMENT_NODE:
            continue
        if n.getAttribute("multiple") == "true":
            # node with multiple children:
            # put them in a list
            l = []
            for c in n.childNodes:
                if c.nodeType != n.ELEMENT_NODE:
                continue
            l.append(nodeToDic(c))
                dic.update({n.nodeName:l})
            continue
    
        try:
            text = getTextFromNode(n)
        except NotTextNodeError:
                # 'normal' node
                dic.update({n.nodeName:nodeToDic(n)})
                continue
    
            # text node
            dic.update({n.nodeName:text})
        continue
        return dic
    
    
    def readConfig(filename):
        dom = parse(filename)
        return nodeToDic(dom)
    
    
    
    
    
    def test():
        dic = readConfig("sample.xml")
    
        print dic["Config"]["Name"]
        print
        for item in dic["Config"]["Items"]:
        print "Item's Name:", item["Name"]
        print "Item's Value:", item["Value"]
    
    test()
    
    
    
    ==================================================
    sample.xml:
    ==================================================
    <?xml version="1.0" encoding="UTF-8"?>
    
    <Config>
        <Name>My Config File</Name>
    
        <Items multiple="true">
        <Item>
            <Name>First Item</Name>
            <Value>Value 1</Value>
        </Item>
        <Item>
            <Name>Second Item</Name>
            <Value>Value 2</Value>
        </Item>
        </Items>
    
    </Config>
    
    
    
    ==================================================
    output:
    ==================================================
    My Config File
    
    Item's Name: First Item
    Item's Value: Value 1
    Item's Name: Second Item
    Item's Value: Value 2
    

    【讨论】:

    • 是的。已在此处复制代码以防万一。
    【解决方案3】:

    这是某人创建的一个很棒的模块。我已经用过好几次了。 http://code.activestate.com/recipes/410469-xml-as-dictionary/

    这是来自网站的代码,以防链接出错。

    from xml.etree import cElementTree as ElementTree
    
    class XmlListConfig(list):
        def __init__(self, aList):
            for element in aList:
                if element:
                    # treat like dict
                    if len(element) == 1 or element[0].tag != element[1].tag:
                        self.append(XmlDictConfig(element))
                    # treat like list
                    elif element[0].tag == element[1].tag:
                        self.append(XmlListConfig(element))
                elif element.text:
                    text = element.text.strip()
                    if text:
                        self.append(text)
    
    
    class XmlDictConfig(dict):
        '''
        Example usage:
    
        >>> tree = ElementTree.parse('your_file.xml')
        >>> root = tree.getroot()
        >>> xmldict = XmlDictConfig(root)
    
        Or, if you want to use an XML string:
    
        >>> root = ElementTree.XML(xml_string)
        >>> xmldict = XmlDictConfig(root)
    
        And then use xmldict for what it is... a dict.
        '''
        def __init__(self, parent_element):
            if parent_element.items():
                self.update(dict(parent_element.items()))
            for element in parent_element:
                if element:
                    # treat like dict - we assume that if the first two tags
                    # in a series are different, then they are all different.
                    if len(element) == 1 or element[0].tag != element[1].tag:
                        aDict = XmlDictConfig(element)
                    # treat like list - we assume that if the first two tags
                    # in a series are the same, then the rest are the same.
                    else:
                        # here, we put the list in dictionary; the key is the
                        # tag name the list elements all share in common, and
                        # the value is the list itself 
                        aDict = {element[0].tag: XmlListConfig(element)}
                    # if the tag has attributes, add those to the dict
                    if element.items():
                        aDict.update(dict(element.items()))
                    self.update({element.tag: aDict})
                # this assumes that if you've got an attribute in a tag,
                # you won't be having any text. This may or may not be a 
                # good idea -- time will tell. It works for the way we are
                # currently doing XML configuration files...
                elif element.items():
                    self.update({element.tag: dict(element.items())})
                # finally, if there are no child tags and no attributes, extract
                # the text
                else:
                    self.update({element.tag: element.text})
    

    示例用法:

    tree = ElementTree.parse('your_file.xml')
    root = tree.getroot()
    xmldict = XmlDictConfig(root)
    

    //或者,如果你想使用 XML 字符串:

    root = ElementTree.XML(xml_string)
    xmldict = XmlDictConfig(root)
    

    【讨论】:

    • 你也可以使用'xmltodict'
    • 我试过这个,它比 xmltodict 快得多。解析一个 80MB 的 xml 文件需要 7 秒,使用 xmltodict 需要 90 秒
    • 你好,这很完美,将为那些找不到cElementTree的人添加一个sn-p,只需将第一行更改为:from xml.etree import cElementTree as ElementTree
    • 投反对票,因为下面发布了更好的答案,特别是在处理具有相同名称的多个标签时。
    • 在旁注中,如果您不需要 使用 Python 并且只是尝试将 XML 作为结构化对象导入以进行操作,我发现它更容易根据thisthis 仅使用R。如果您只是运行library("XML"); result &lt;- xmlParse(file = "file.xml"); xml_data &lt;- xmlToList(result),您将把您的 XML 作为嵌套列表导入。具有相同名称的多个标签很好,标签属性成为一个额外的列表项。
    【解决方案4】:

    最新版本的 PicklingTools 库(1.3.0 和 1.3.1)支持从 XML 转换为 Python 字典的工具。

    可在此处下载:PicklingTools 1.3.1

    转换器here 有很多文档:文档详细描述了在 XML 和 Python 字典之间转换时会出现的所有决策和问题(有许多边缘情况:属性、列表,匿名列表,匿名字典,评估等大多数转换器不处理)。不过,总的来说, 转换器易于使用。如果“example.xml”包含:

    <top>
      <a>1</a>
      <b>2.2</b>
      <c>three</c>
    </top>
    

    然后将其转换为字典:

    >>> from xmlloader import *
    >>> example = file('example.xml', 'r')   # A document containing XML
    >>> xl = StreamXMLLoader(example, 0)     # 0 = all defaults on operation
    >>> result = xl.expect XML()
    >>> print result
    {'top': {'a': '1', 'c': 'three', 'b': '2.2'}}
    

    在 C++ 和 Python 中都有用于转换的工具:C++ 和 Python 进行相同的转换,但 C++ 的速度大约快 60 倍

    【讨论】:

    • 当然,如果有2个a,这不是一个好的格式。
    • 看起来很有趣,但我还没有弄清楚 PicklingTools 的用途——这只是一个源代码文件的压缩包,我必须从中找到适合我工作的源代码文件,然后将它们复制到我的项目中?没有要加载的模块或更简单的东西?
    • 我得到:在 peekIntoNextNWSChar c = self.is.read(1) AttributeError: 'str' object has no attribute 'read'
    【解决方案5】:

    在某一时刻,我不得不解析和编写只包含没有属性的元素的 XML,因此从 XML 到 dict 的 1:1 映射很容易。这是我想出的,以防其他人也不需要属性:

    def xmltodict(element):
        if not isinstance(element, ElementTree.Element):
            raise ValueError("must pass xml.etree.ElementTree.Element object")
    
        def xmltodict_handler(parent_element):
            result = dict()
            for element in parent_element:
                if len(element):
                    obj = xmltodict_handler(element)
                else:
                    obj = element.text
    
                if result.get(element.tag):
                    if hasattr(result[element.tag], "append"):
                        result[element.tag].append(obj)
                    else:
                        result[element.tag] = [result[element.tag], obj]
                else:
                    result[element.tag] = obj
            return result
    
        return {element.tag: xmltodict_handler(element)}
    
    
    def dicttoxml(element):
        if not isinstance(element, dict):
            raise ValueError("must pass dict type")
        if len(element) != 1:
            raise ValueError("dict must have exactly one root key")
    
        def dicttoxml_handler(result, key, value):
            if isinstance(value, list):
                for e in value:
                    dicttoxml_handler(result, key, e)
            elif isinstance(value, basestring):
                elem = ElementTree.Element(key)
                elem.text = value
                result.append(elem)
            elif isinstance(value, int) or isinstance(value, float):
                elem = ElementTree.Element(key)
                elem.text = str(value)
                result.append(elem)
            elif value is None:
                result.append(ElementTree.Element(key))
            else:
                res = ElementTree.Element(key)
                for k, v in value.items():
                    dicttoxml_handler(res, k, v)
                result.append(res)
    
        result = ElementTree.Element(element.keys()[0])
        for key, value in element[element.keys()[0]].items():
            dicttoxml_handler(result, key, value)
        return result
    
    def xmlfiletodict(filename):
        return xmltodict(ElementTree.parse(filename).getroot())
    
    def dicttoxmlfile(element, filename):
        ElementTree.ElementTree(dicttoxml(element)).write(filename)
    
    def xmlstringtodict(xmlstring):
        return xmltodict(ElementTree.fromstring(xmlstring).getroot())
    
    def dicttoxmlstring(element):
        return ElementTree.tostring(dicttoxml(element))
    

    【讨论】:

      【解决方案6】:

      以下 XML-to-Python-dict sn-p 解析实体以及 this XML-to-JSON "specification" 之后的属性。它是处理所有 XML 情况的最通用的解决方案。

      from collections import defaultdict
      
      def etree_to_dict(t):
          d = {t.tag: {} if t.attrib else None}
          children = list(t)
          if children:
              dd = defaultdict(list)
              for dc in map(etree_to_dict, children):
                  for k, v in dc.items():
                      dd[k].append(v)
              d = {t.tag: {k:v[0] if len(v) == 1 else v for k, v in dd.items()}}
          if t.attrib:
              d[t.tag].update(('@' + k, v) for k, v in t.attrib.items())
          if t.text:
              text = t.text.strip()
              if children or t.attrib:
                  if text:
                    d[t.tag]['#text'] = text
              else:
                  d[t.tag] = text
          return d
      

      使用:

      from xml.etree import cElementTree as ET
      e = ET.XML('''
      <root>
        <e />
        <e>text</e>
        <e name="value" />
        <e name="value">text</e>
        <e> <a>text</a> <b>text</b> </e>
        <e> <a>text</a> <a>text</a> </e>
        <e> text <a>text</a> </e>
      </root>
      ''')
      
      from pprint import pprint
      pprint(etree_to_dict(e))
      

      这个例子的输出(根据上面链接的“规范”)应该是:

      {'root': {'e': [None,
                      'text',
                      {'@name': 'value'},
                      {'#text': 'text', '@name': 'value'},
                      {'a': 'text', 'b': 'text'},
                      {'a': ['text', 'text']},
                      {'#text': 'text', 'a': 'text'}]}}
      

      不一定漂亮,但它是明确的,更简单的 XML 输入会导致更简单的 JSON。 :)


      更新

      如果你想做 reverse,从 JSON/dict 发出 XML 字符串,你可以使用:

      try:
        basestring
      except NameError:  # python3
        basestring = str
      
      def dict_to_etree(d):
          def _to_etree(d, root):
              if not d:
                  pass
              elif isinstance(d, basestring):
                  root.text = d
              elif isinstance(d, dict):
                  for k,v in d.items():
                      assert isinstance(k, basestring)
                      if k.startswith('#'):
                          assert k == '#text' and isinstance(v, basestring)
                          root.text = v
                      elif k.startswith('@'):
                          assert isinstance(v, basestring)
                          root.set(k[1:], v)
                      elif isinstance(v, list):
                          for e in v:
                              _to_etree(e, ET.SubElement(root, k))
                      else:
                          _to_etree(v, ET.SubElement(root, k))
              else:
                  raise TypeError('invalid type: ' + str(type(d)))
          assert isinstance(d, dict) and len(d) == 1
          tag, body = next(iter(d.items()))
          node = ET.Element(tag)
          _to_etree(body, node)
          return ET.tostring(node)
      
      pprint(dict_to_etree(d))
      

      【讨论】:

      • 感谢此代码!附加信息:如果您使用 python 2.5,则不能使用字典理解,因此您必须将行 d = {t.tag: {k:v[0] if len(v) == 1 else v for k, v in dd.iteritems()}} 更改为 d = { t.tag: dict( (k, v[0] if len(v) == 1 else v) for k, v in dd.iteritems() ) }
      • 我为此测试了近 10 个 sn-ps / python 模块 / 等。这是我找到的最好的。根据我的测试,它:1) 比 github.com/martinblech/xmltodict 快得多(基于 XML SAX api)2) 比 github.com/mcspring/XML2Dict 好,当几个孩子有相同的名字时会有一些小问题 3) 比 code.activestate.com/recipes/410469-xml-as-dictionary 好小问题也更重要:4)比以前的所有代码都短得多!谢谢@K3---rnc
      • 这是迄今为止最全面的答案,它适用于 > 2.6,并且相当灵活。我唯一的问题是文本可以根据是否有属性来改变它的位置)。我还发布了一个更小、更严格的解决方案。
      • 如果您需要从 XML 文件中获取有序的 dict,请使用相同的示例,只需稍作修改(请参阅下面的回复):stackoverflow.com/questions/2148119/…
      • 这在与cElementTreelxml.etree 一起使用时也非常漂亮和快速。请注意,使用 Python 3 时,所有 .iteritems() 都必须更改为 .items()(行为相同,但关键字从 Python 2 更改为 3)。
      【解决方案7】:

      xmltodict(完全披露:我写的)正是这样做的:

      xmltodict.parse("""
      <?xml version="1.0" ?>
      <person>
        <name>john</name>
        <age>20</age>
      </person>""")
      # {u'person': {u'age': u'20', u'name': u'john'}}
      

      【讨论】:

      • 这是一个很棒的模块。
      • 您为我节省了很多精力。让我开心。
      • 另外,对于未来的 googlenauts - 我能够在 App Engine 中使用它,我一直认为它不能很好地与 Python 中的大多数 xml 库配合使用。
      • u 只是表示它是存储的 unicode 字符串。它不会以任何方式影响字符串的值。
      • 不错。是的,@ypercube,反向有一个 xmldict.unparse() 函数。
      【解决方案8】:
      def xml_to_dict(node):
          u''' 
          @param node:lxml_node
          @return: dict 
          '''
      
          return {'tag': node.tag, 'text': node.text, 'attrib': node.attrib, 'children': {child.tag: xml_to_dict(child) for child in node}}
      

      【讨论】:

        【解决方案9】:

        @dibrovsd:如果 xml 有多个同名标签,解决方案将不起作用

        根据您的想法,我对代码做了一些修改,并将其编写为通用节点而不是根:

        from collections import defaultdict
        def xml2dict(node):
            d, count = defaultdict(list), 1
            for i in node:
                d[i.tag + "_" + str(count)]['text'] = i.findtext('.')[0]
                d[i.tag + "_" + str(count)]['attrib'] = i.attrib # attrib gives the list
                d[i.tag + "_" + str(count)]['children'] = xml2dict(i) # it gives dict
             return d
        

        【讨论】:

          【解决方案10】:

          http://code.activestate.com/recipes/410469-xml-as-dictionary/ 中的代码运行良好,但如果在层次结构中的给定位置有多个相同的元素,它只会覆盖它们。

          我在两者之间添加了一个垫片,以查看该元素在 self.update() 之前是否已经存在。如果是这样,则弹出现有条目并从现有条目和新条目中创建一个列表。任何后续重复项都会添加到列表中。

          不确定这是否可以更优雅地处理,但它有效:

          import xml.etree.ElementTree as ElementTree
          
          class XmlDictConfig(dict):
              def __init__(self, parent_element):
                  if parent_element.items():
                      self.updateShim(dict(parent_element.items()))
                  for element in parent_element:
                      if len(element):
                          aDict = XmlDictConfig(element)
                          if element.items():
                              aDict.updateShim(dict(element.items()))
                          self.updateShim({element.tag: aDict})
                      elif element.items():
                          self.updateShim({element.tag: dict(element.items())})
                      else:
                          self.updateShim({element.tag: element.text.strip()})
          
              def updateShim (self, aDict ):
                  for key in aDict.keys():
                      if key in self:
                          value = self.pop(key)
                          if type(value) is not list:
                              listOfDicts = []
                              listOfDicts.append(value)
                              listOfDicts.append(aDict[key])
                              self.update({key: listOfDicts})
          
                          else:
                              value.append(aDict[key])
                              self.update({key: value})
                      else:
                          self.update(aDict)
          

          【讨论】:

            【解决方案11】:

            这个轻量级版本虽然不可配置,但很容易根据需要进行定制,并且可以在旧 python 中工作。它也是刚性的——意味着无论属性是否存在,结果都是相同的。

            import xml.etree.ElementTree as ET
            
            from copy import copy
            
            def dictify(r,root=True):
                if root:
                    return {r.tag : dictify(r, False)}
                d=copy(r.attrib)
                if r.text:
                    d["_text"]=r.text
                for x in r.findall("./*"):
                    if x.tag not in d:
                        d[x.tag]=[]
                    d[x.tag].append(dictify(x,False))
                return d
            

            所以:

            root = ET.fromstring("<erik><a x='1'>v</a><a y='2'>w</a></erik>")
            
            dictify(root)
            

            结果:

            {'erik': {'a': [{'x': '1', '_text': 'v'}, {'y': '2', '_text': 'w'}]}}
            

            【讨论】:

            • 我喜欢这个解决方案。简单,不需要外部库。
            • 我也喜欢这个答案,因为它就在我面前(没有外部链接)。干杯!
            【解决方案12】:

            你可以用 lxml 很容易地做到这一点。首先安装它:

            [sudo] pip install lxml
            

            这是我编写的一个递归函数,可以为您完成繁重的工作:

            from lxml import objectify as xml_objectify
            
            
            def xml_to_dict(xml_str):
                """ Convert xml to dict, using lxml v3.4.2 xml processing library """
                def xml_to_dict_recursion(xml_object):
                    dict_object = xml_object.__dict__
                    if not dict_object:
                        return xml_object
                    for key, value in dict_object.items():
                        dict_object[key] = xml_to_dict_recursion(value)
                    return dict_object
                return xml_to_dict_recursion(xml_objectify.fromstring(xml_str))
            
            xml_string = """<?xml version="1.0" encoding="UTF-8"?><Response><NewOrderResp>
            <IndustryType>Test</IndustryType><SomeData><SomeNestedData1>1234</SomeNestedData1>
            <SomeNestedData2>3455</SomeNestedData2></SomeData></NewOrderResp></Response>"""
            
            print xml_to_dict(xml_string)
            

            以下变体保留了父键/元素:

            def xml_to_dict(xml_str):
                """ Convert xml to dict, using lxml v3.4.2 xml processing library, see http://lxml.de/ """
                def xml_to_dict_recursion(xml_object):
                    dict_object = xml_object.__dict__
                    if not dict_object:  # if empty dict returned
                        return xml_object
                    for key, value in dict_object.items():
                        dict_object[key] = xml_to_dict_recursion(value)
                    return dict_object
                xml_obj = objectify.fromstring(xml_str)
                return {xml_obj.tag: xml_to_dict_recursion(xml_obj)}
            

            如果你只想返回一个子树并将其转换为dict,你可以使用Element.find()获取子树然后进行转换:

            xml_obj.find('.//')  # lxml.objectify.ObjectifiedElement instance
            

            请参阅 lxml 文档 here。我希望这会有所帮助!

            【讨论】:

              【解决方案13】:

              来自@K3---rnc response(对我来说最好的)我添加了一些小修改以从 XML 文本中获取 OrderedDict(有时顺序很重要):

              def etree_to_ordereddict(t):
              d = OrderedDict()
              d[t.tag] = OrderedDict() if t.attrib else None
              children = list(t)
              if children:
                  dd = OrderedDict()
                  for dc in map(etree_to_ordereddict, children):
                      for k, v in dc.iteritems():
                          if k not in dd:
                              dd[k] = list()
                          dd[k].append(v)
                  d = OrderedDict()
                  d[t.tag] = OrderedDict()
                  for k, v in dd.iteritems():
                      if len(v) == 1:
                          d[t.tag][k] = v[0]
                      else:
                          d[t.tag][k] = v
              if t.attrib:
                  d[t.tag].update(('@' + k, v) for k, v in t.attrib.iteritems())
              if t.text:
                  text = t.text.strip()
                  if children or t.attrib:
                      if text:
                          d[t.tag]['#text'] = text
                  else:
                      d[t.tag] = text
              return d
              

              以下@K3---rnc示例,你可以使用它:

              from xml.etree import cElementTree as ET
              e = ET.XML('''
              <root>
                <e />
                <e>text</e>
                <e name="value" />
                <e name="value">text</e>
                <e> <a>text</a> <b>text</b> </e>
                <e> <a>text</a> <a>text</a> </e>
                <e> text <a>text</a> </e>
              </root>
              ''')
              
              from pprint import pprint
              pprint(etree_to_ordereddict(e))
              

              希望对你有所帮助;)

              【讨论】:

                【解决方案14】:

                免责声明: 这个修改过的 XML 解析器的灵感来自 Adam Clark 原始 XML 解析器适用于大多数简单情况。但是,它不适用于一些复杂的 XML 文件。我逐行调试代码,最后修复了一些问题。如果您发现一些错误,请告诉我。我很高兴修复它。

                class XmlDictConfig(dict):  
                    '''   
                    Note: need to add a root into if no exising    
                    Example usage:
                    >>> tree = ElementTree.parse('your_file.xml')
                    >>> root = tree.getroot()
                    >>> xmldict = XmlDictConfig(root)
                    Or, if you want to use an XML string:
                    >>> root = ElementTree.XML(xml_string)
                    >>> xmldict = XmlDictConfig(root)
                    And then use xmldict for what it is... a dict.
                    '''
                    def __init__(self, parent_element):
                        if parent_element.items():
                            self.updateShim( dict(parent_element.items()) )
                        for element in parent_element:
                            if len(element):
                                aDict = XmlDictConfig(element)
                            #   if element.items():
                            #   aDict.updateShim(dict(element.items()))
                                self.updateShim({element.tag: aDict})
                            elif element.items():    # items() is specialy for attribtes
                                elementattrib= element.items()
                                if element.text:           
                                    elementattrib.append((element.tag,element.text ))     # add tag:text if there exist
                                self.updateShim({element.tag: dict(elementattrib)})
                            else:
                                self.updateShim({element.tag: element.text})
                
                    def updateShim (self, aDict ):
                        for key in aDict.keys():   # keys() includes tag and attributes
                            if key in self:
                                value = self.pop(key)
                                if type(value) is not list:
                                    listOfDicts = []
                                    listOfDicts.append(value)
                                    listOfDicts.append(aDict[key])
                                    self.update({key: listOfDicts})
                                else:
                                    value.append(aDict[key])
                                    self.update({key: value})
                            else:
                                self.update({key:aDict[key]})  # it was self.update(aDict)    
                

                【讨论】:

                  【解决方案15】:

                  我有一个从 lxml 元素中获取字典的递归方法

                      def recursive_dict(element):
                          return (element.tag.split('}')[1],
                                  dict(map(recursive_dict, element.getchildren()),
                                       **element.attrib))
                  

                  【讨论】:

                  • 此解决方案缺少一些代码,例如导入和设置。我收到消息“str”对象没有属性“tag”
                  【解决方案16】:

                  我已根据自己的喜好修改了其中一个答案,并使用相同标签处理多个值,例如考虑保存在 XML.xml 文件中的以下 xml 代码

                       <A>
                          <B>
                              <BB>inAB</BB>
                              <C>
                                  <D>
                                      <E>
                                          inABCDE
                                      </E>
                                      <E>value2</E>
                                      <E>value3</E>
                                  </D>
                                  <inCout-ofD>123</inCout-ofD>
                              </C>
                          </B>
                          <B>abc</B>
                          <F>F</F>
                      </A>
                  

                  在python中

                  import xml.etree.ElementTree as ET
                  
                  
                  
                  
                  class XMLToDictionary(dict):
                      def __init__(self, parentElement):
                          self.parentElement = parentElement
                          for child in list(parentElement):
                              child.text = child.text if (child.text != None) else  ' '
                              if len(child) == 0:
                                  self.update(self._addToDict(key= child.tag, value = child.text.strip(), dict = self))
                              else:
                                  innerChild = XMLToDictionary(parentElement=child)
                                  self.update(self._addToDict(key=innerChild.parentElement.tag, value=innerChild, dict=self))
                  
                      def getDict(self):
                          return {self.parentElement.tag: self}
                  
                      class _addToDict(dict):
                          def __init__(self, key, value, dict):
                              if not key in dict:
                                  self.update({key: value})
                              else:
                                  identical = dict[key] if type(dict[key]) == list else [dict[key]]
                                  self.update({key: identical + [value]})
                  
                  
                  tree = ET.parse('./XML.xml')
                  root = tree.getroot()
                  parseredDict = XMLToDictionary(root).getDict()
                  print(parseredDict)
                  

                  输出是

                  {'A': {'B': [{'BB': 'inAB', 'C': {'D': {'E': ['inABCDE', 'value2', 'value3']}, 'inCout-ofD': '123'}}, 'abc'], 'F': 'F'}}
                  

                  【讨论】:

                    【解决方案17】:

                    我写了一个简单的递归函数来完成这项工作:

                    from xml.etree import ElementTree
                    root = ElementTree.XML(xml_to_convert)
                    
                    def xml_to_dict_recursive(root):
                    
                        if len(root.getchildren()) == 0:
                            return {root.tag:root.text}
                        else:
                            return {root.tag:list(map(xml_to_dict_recursive, root.getchildren()))}
                    

                    【讨论】:

                    • 迄今为止最简单的解决方案!
                    【解决方案18】:

                    另一种选择(为层次结构中的相同标签构建列表):

                    from xml.etree import cElementTree as ElementTree
                    
                    def xml_to_dict(xml, result):
                        for child in xml:
                            if len(child) == 0:
                                result[child.tag] = child.text
                            else:
                                if child.tag in result:
                                    if not isinstance(result[child.tag], list):
                                        result[child.tag] = [result[child.tag]]
                                    result[child.tag].append(xml_to_dict(child, {}))
                                else:
                                    result[child.tag] = xml_to_dict(child, {})
                        return result
                    
                    xmlTree = ElementTree.parse('my_file.xml')
                    xmlRoot = xmlTree.getroot()
                    dictRoot = xml_to_dict(xmlRoot, {})
                    result = {xmlRoot.tag: dictRoot}
                    
                    

                    【讨论】:

                      【解决方案19】:

                      超级简单的代码 #按照这个,很简单,不需要任何东西,将 XML 转换为字符串并使用 find 命令找到您要查找的单词,如下所示 #希望这很简单

                      def xml_key(key, text1):
                          tx1 = "<" + key + ">"
                          tx2 = "</" + key + ">"  
                          tx = text1.find(tx1)
                          ty = text1.find(tx2)
                          tx = tx + len(tx1)
                          tw = text1[tx:ty]
                          return(tw)
                      
                      text1 = "<person><name>john</name><age>20</age></person>"                         
                      dict1 = {"name": xml_key("name",text1),"age":xml_key("age",text1)}
                      
                      print(dict1)
                      

                      输出: {'name': '约翰'}

                      【讨论】:

                        猜你喜欢
                        • 2014-05-21
                        • 2019-11-09
                        • 1970-01-01
                        • 1970-01-01
                        • 1970-01-01
                        • 2016-12-06
                        • 2021-07-25
                        相关资源
                        最近更新 更多