简体   繁体   English

从python字典生成XML

[英]Generating XML from python dictionary

I have python dictionaries with the following structure: 我有以下结构的python字典:

d={ 'cfdi:Emisor': {'rfc': u'ALF040329CX6', 'nombre': u'ALFATECH, S.A. DE C.V.', 
     'cfdi:RegimenFiscal': {'Regimen': u'Personas morales del r\xe9gimen general'}, 
      'cfdi:ExpedidoEn': {'calle': u'ING. INDUSTRIALES', 'localidad': u'MORELIA', 'pais':   u'M\xe9xico', 'noInterior': 'N/A', 'colonia': u'BUENAVISTA 1A ETAPA', 'noExterior': u'215', 'codigoPostal': u'58228', 'estado': u'Michoac\xe1n', 'municipio': u'MORELIA'}, 
       'cfdi:DomicilioFiscal': {'calle': u'ING. INDUSTRIALES', 'localidad': u'MORELIA', 'pais': u'M\xe9xico', 'noInterior': 'N/A', 'colonia': u'BUENAVISTA 1A ETAPA', 'noExterior': u'215', 'codigoPostal': u'58228', 'estado': u'Michoac\xe1n', 'municipio': u'MORELIA'}, 
        }}

The structure consists of dictionaries inside a dictionary. 该结构由字典中的字典组成。 I need to convert it to XML, then I got the following code to achieve that: 我需要将其转换为XML,然后获得以下代码来实现:

def dict2xml(data_dict, node=False, doc=False):

    parent = False
    if node:
        parent = True

    for element, attribute in data_dict.iteritems():
        if not parent:
            doc = minidom.Document()
        if isinstance(attribute, dict):
            if not parent:
                node = doc.createElement(element)
                dict2xml(attribute, node, doc)
            else:
                child = doc.createElement(element)
                dict2xml(attribute, child, doc)
                node.appendChild(child)
        elif isinstance(attribute, list):
            child = doc.createElement(element)
            for attr in attribute:
                if isinstance(attr, dict):
                    dict2xml(attr, child, doc)
            node.appendChild(child)
        else:
            if isinstance(attribute, str) or isinstance(attribute, unicode):
                attribute = str(attribute)
            else:
                    attribute = str(attribute)
            node.setAttribute(element, attribute)
            # print "attribute",unicode( attribute, 'UTF-8')
    if not parent:
        doc.appendChild(node)
    print doc.toprettyxml(indent="     ", encoding='utf-8')
    return doc

This function works just fine with the previous dictionary and returns this: 此函数与上一个字典配合使用并返回以下内容:

<cfdi:Emisor nombre="ALFATECH, S.A. DE C.V." rfc="ALF040329CX6">
    <cfdi:RegimenFiscal Regimen="Personas morales del régimen general"/>
    <cfdi:ExpedidoEn calle="ING. INDUSTRIALES" codigoPostal="58228" colonia="BUENAVISTA 1A ETAPA" estado="Michoacán" localidad="MORELIA" municipio="MORELIA" noExterior="215" noInterior="N/A" pais="México"/>
    <cfdi:DomicilioFiscal calle="ING. INDUSTRIALES" codigoPostal="58228" colonia="BUENAVISTA 1A ETAPA" estado="Michoacán" localidad="MORELIA" municipio="MORELIA" noExterior="215" noInterior="N/A" pais="México"/>
</cfdi:Emisor>

which is what I was waiting, but now I need to do the same with this dictionary that has basically the same structure but contains different elements: 这是我一直在等待的东西,但是现在我需要对这本结构基本相同但包含不同元素的字典进行相同的操作:

d3={ 'catalogocuentas:Catalogo': {'rfc': u'ALF040329CX6', 'xmlns:catalogocuentas':'"http://www.sat.gob.mx/catalogocuentas"', 'xmlns:xsi':'"http://www.w3.org/2001/XMLSchema-instance"', 'xsi:schemaLocation':'"http://www.sat.gob.mx//catalogocuentas"', 'Ano':'2014', 'Mes':'02', 'TotalCtas':'219','version':'1.0', 
     'catalogocuentas:Ctas': {'Natur': u'D', 'nivel':'2', 'SubCtaDe':'1110-000-000', 'Desc':'CAJA CHICA', 'NumCta':'1110-001-000', 'CodAgrup':'1.1'}, 
      'catalogocuentas:Ctas': {'Natur': u'D', 'nivel':'3', 'SubCtaDe':'1120-001-000', 'Desc':'Banamex 741107-1', 'NumCta':'1120-001-001', 'CodAgrup':'2.1'}, 
       'catalogocuentas:Ctas': {'Natur': u'D', 'nivel':'3', 'SubCtaDe':'1120-001-000', 'Desc':'Bancomer 12911256971', 'NumCta':'1120-001-002', 'CodAgrup':'2.1'} 
        }}

You guys can see that the main difference is that this dictionary has THE SAME TAG for the inner elements(dictionaries), and I got the following result: 大家可以看到,主要的区别是该词典的内部元素(词典)具有THE SAME TAG,并且得到以下结果:

<?xml version="1.0" encoding="utf-8"?>
   <catalogocuentas:Catalogo Ano="2014" Mes="02" TotalCtas="219" rfc="ALF040329CX6"     version="1.0" xmlns:catalogocuentas="&quot;http://www.sat.gob.mx/catalogocuentas&quot;" xmlns:xsi="&quot;http://www.w3.org/2001/XMLSchema-instance&quot;" xsi:schemaLocation="&quot;http://www.sat.gob.mx//catalogocuentas&quot;">
         <catalogocuentas:Ctas CodAgrup="2.1" Desc="Bancomer 12911256971" Natur="D" NumCta="1120-001-002" SubCtaDe="1120-001-000" nivel="3"/>
   </catalogocuentas:Catalogo>

As you can see I only got THE LAST element(dictionary) from d3, and not all of them. 如您所见,我仅从d3中获得了LAST元素(词典),而并非全部。

From this I can conclude that the problem might be that in this dictionary I have the same tag for all my elements. 由此我可以得出结论,问题可能出在该字典中,我所有元素的标签都相同。

Is this the real problem? 这是真正的问题吗?

Any suggestions will be highly appreciated!!! 任何建议将不胜感激!!!

Thanks in advance. 提前致谢。

Your code formatting makes my eyes hurt. 您的代码格式使我的眼睛受伤。 The Python formatting style guide is your friend. Python 格式样式指南是您的朋友。 In particular you abuse the maximum line length. 特别是您滥用最大行长。

If you want to format python dictionaries into XML, there are a number of standard options. 如果要将python字典格式化为XML,可以使用许多标准选项。 See this previous question for some solutions. 有关某些解决方案,请参见前面的问题。

Python dictionary only allow a single element based on the same key, duplicate entries overwrite previous entries. Python字典仅允许基于相同键的单个元素,重复的条目会覆盖以前的条目。

There are libraries that already exist for this, one of which I created . 为此已经有一些库, 我创建了其中一个 Using the xmler package you can take your dictionary and turn it in to the following xml pretty easily: 使用xmler包,您可以轻松地将字典放入下面的xml中:

<?xml version="1.0" encoding="UTF-8"?><?xml version="1.0" ?>
<cfdi:Emisor xmlns:cfdi="www.sat.gob.mx/cfd/3">
    <cfdi:DomicilioFiscal>
        <pais>Mexico</pais>
        <localidad>MORELIA</localidad>
        <estado>Michoacán</estado>
        <noExterior>215</noExterior>
        <municipio>MORELIA</municipio>
        <calle>ING. INDUSTRIALES</calle>
        <noInterior>N/A</noInterior>
        <codigoPostal>58228</codigoPostal>
        <colonia>BUENAVISTA 1A ETAPA</colonia>
    </cfdi:DomicilioFiscal>
    <nombre>ALFATECH, S.A. DE C.V.</nombre>
    <cfdi:ExpedidoEn>
        <pais>Mexico</pais>
        <localidad>MORELIA</localidad>
        <estado>Michoacán</estado>
        <noExterior>215</noExterior>
        <municipio>MORELIA</municipio>
        <calle>ING. INDUSTRIALES</calle>
        <noInterior>N/A</noInterior>
        <codigoPostal>58228</codigoPostal>
        <colonia>BUENAVISTA 1A ETAPA</colonia>
    </cfdi:ExpedidoEn>
    <rfc>ALF040329CX6</rfc>
    <cfdi:RegimenFiscal>
        <Regimen>Personas morales del régimen general</Regimen>
    </cfdi:RegimenFiscal>
</cfdi:Emisor>

To get that XML structure and to get it to pretty print correctly you'll have to modify your dictionary a little bit as you currently aren't defining the xml namespaces for cfdi . 为了获得该XML结构并使其正确打印,您需要对字典进行一些修改,因为您当前没有为cfdi定义xml名称空间。 I used the following dictionary: 我使用以下字典:

d = {
    "cfdi:Emisor": {
        "@attrs": {
            "xmlns:cfdi": "www.sat.gob.mx/cfd/3"
        },
        "rfc": "ALF040329CX6",
        "nombre": "ALFATECH, S.A. DE C.V.",
        "cfdi:RegimenFiscal": {
            "Regimen": "Personas morales del r\xe9gimen general"
        },
        "cfdi:ExpedidoEn": {
            "calle": "ING. INDUSTRIALES",
            "localidad": "MORELIA",
            "pais": "Mexico",
            "noInterior": "N/A",
            "colonia": "BUENAVISTA 1A ETAPA",
            "noExterior": "215",
            "codigoPostal": "58228",
            "estado": "Michoac\xe1n",
            "municipio": "MORELIA"
        },
        "cfdi:DomicilioFiscal": {
            "calle": "ING. INDUSTRIALES",
            "localidad": "MORELIA",
            "pais": "Mexico",
            "noInterior": "N/A",
            "colonia": "BUENAVISTA 1A ETAPA",
            "noExterior": "215",
            "codigoPostal": "58228",
            "estado": "Michoac\xe1n",
            "municipio": "MORELIA"
        }
    }
}

and then ran 然后跑

print(xmler(d, customRoot=None, pretty=True))

Notice the @attrs key in the dictionary. 注意字典中的@attrs键。 It adds attributes to an element. 它将属性添加到元素。 You could also use the @ns key here to set the namespaces rather than using a colon separated string. 您也可以在此处使用@ns键来设置名称空间,而不是使用冒号分隔的字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM