zoukankan      html  css  js  c++  java
  • xml -> dict

    这个工具没有自己写, github上有现成的。详细代码如下:

    
    #!/usr/bin/env python
    # encoding: utf-8
    '''
    XML2Dict: Convert xml string to python dict
    @author: Mc.Spring
    @contact: Heresy.Mc@gmail.com
    @since: Created on 2009-5-18
    @todo: Add namespace support
    @copyright: Copyright (C) 2009 MC.Spring Team. All rights reserved.
    @license: http://www.apache.org/licenses/LICENSE-2.0 Apache License
    '''
    
    try:
        import xml.etree.ElementTree as ET
    except:
        import cElementTree as ET # for 2.4
    
    
    __all__ = ['XML2Dict']
    
    
    class XML2Dict(object):
    
        def __init__(self, coding='UTF-8'):
            self._coding = coding
    
        def _parse_node(self, node):
            tree = {}
    
            #Save childrens
            for child in node.getchildren():
                ctag = child.tag
                cattr = child.attrib
                ctext = child.text.strip().encode(self._coding) if child.text is not None else ''
                ctree = self._parse_node(child)
    
                if not ctree:
                    cdict = self._make_dict(ctag, ctext, cattr)
                else:
                    cdict = self._make_dict(ctag, ctree, cattr)
    
                if ctag not in tree: # First time found
                    tree.update(cdict)
                    continue
    
                atag = '@' + ctag
                atree = tree[ctag]
                if not isinstance(atree, list):
                    if not isinstance(atree, dict):
                        atree = {}
    
                    if atag in tree:
                        atree['#'+ctag] = tree[atag]
                        del tree[atag]
    
                    tree[ctag] = [atree] # Multi entries, change to list
    
                if cattr:
                    ctree['#'+ctag] = cattr
    
                tree[ctag].append(ctree)
    
            return  tree
    
        def _make_dict(self, tag, value, attr=None):
            '''Generate a new dict with tag and value
            If attr is not None then convert tag name to @tag
            and convert tuple list to dict
            '''
            ret = {tag: value}
    
            # Save attributes as @tag value
            if attr:
                atag = '@' + tag
    
                aattr = {}
                for k, v in attr.items():
                    aattr[k] = v
    
                ret[atag] = aattr
    
                del atag
                del aattr
    
            return ret
    
        def parse(self, xml):
            '''Parse xml string to python dict
            '''
            EL = ET.fromstring(xml)
    
            return self._make_dict(EL.tag, self._parse_node(EL), EL.attrib)
    

    使用方法:

    1. 安装: pip install XML2Dict , 具体见链接;
    2. 导入: from encoder import XML2Dict
    3. 读取成字符串, 进行解析
  • 相关阅读:
    html5内容嵌入元素
    thinkphp默认路径访问报错
    LNMP安装教程
    wampserver的mysql启动与环境变量设置
    http响应详解_韩顺平PHP视频听课笔记
    http请求详解,防盗链技术_韩顺平PHP视频听课笔记
    使用js写一个作用于xml文件的ajax
    使用js创建一个简单的ajax
    js写一个ajax错误规避
    使用js写一个原生态简单的ajax
  • 原文地址:https://www.cnblogs.com/geoffreyone/p/11724910.html
Copyright © 2011-2022 走看看