Python and the xml format conversion dict

Doing the automation interfaces when the request before the data is in JSON format, Python has a built-in package to solve. Recently doing APP interface, encountered the requested data in XML format, spent a lot of effort to solve, solution is: get a XML interface documentation, online converted into json format (xml template aim is to get the data) , stored in the json file, according to the name of the interface to extract.

  • github original introduction: Python modules using XML feels like you're using JSON
  • Link: https: //github.com/martinblech/xmltodict

    Download xmltodict (pip install xmltodict)

C:\Users\wy.DESKTOP-KENPKKP\Desktop\Dingda\dingAPI>pip  install xmltodict
Requirement already satisfied: xmltodict in c:\users\wy.desktop-kenpkkp\appdata\local\programs\python\python36\lib\site-packages
 (0.12.0)

Directly demonstrated, to prepare a raw XML data

<?xml version="1.0"?>
<mydocument has="an attribute">#has是节点属性
    <and>
        <many>elements</many>
        <many>more elements</many>
    </and>
    <plus a="complex">
        element as well
    </plus>
</mydocument>

Since there is no direct assignment XML will be able to find a method to function, so the first with online conversion tool for converting XML to JSON, as a result of the conversion

{
  "mydocument": {
    "-has": "an attribute",    #在这里要留意一个事情,XML是有节点的,节点有属性,json数据的键之前有-的,就是属性,但是xmltodict识别的属性是@,所以要把-替换为@(就是这里找了好久)
    "and": {
      "many": [
        "elements",
        "more elements"
      ]
    },
    "plus": {
      "-a": "complex",
      "#text": "
        element as well
    "
    }
  }
}

Sample Code

dictdata={
    "mydocument": {
        "@has": "an attribute",
        "and": {
            "many": [
                "elements",
                "more elements"
            ]
        },
        "plus": {
            "@a": "complex",
            "#text": "element as well"
}
}
}
xmldata=xmltodict.unparse(dictdata,pretty=True)#dict转xml
print(xmldata)#解析结果可以复制运行查看,结果是原始数据
new_dictdata=xmltodict.parse(xmldata,process_namespaces = True)#xml转dict
print(new_dictdata)#在这里有一个注意事项,new_dictdata的数据格式是<class 'collections.OrderedDict'>,并不直接是dict,需要自行处理
"""
处理方式如下
"""
key_dictdata=dict(new_dictdata)
value_dictdata=dict(dict(new_dictdata)["mydocument"])
key_dictdata["mydocument"]=value_dictdata
print(key_dictdata)

Xml dict conversion is above and, if necessary conversion json, json built module can be completed, but this is troublesome in the use of automated testing framework, and have poor reusability, good package as

#-*- coding: utf-8 -*
#@author 小测试 
#@create 2019-10-30 15:46
import xmltodict
"""
xml和dict转换
"""
def dict_xml(dictdata):
    """
    dict转xml
    dictstr: dict字符串
    return: xml字符串
    """
    xmlstr=xmltodict.unparse(dictdata, pretty=True)
    return xmlstr
def xml_dict(xmldata,moudle):
    """
    xml转dict
    xmlstr: xml字符串
    moudle:根节点
    return: dict字符串
    """
    data=xmltodict.parse(xmldata,process_namespaces = True)
    dictdata=dict(data)
    _dictdata=dict(dictdata[moudle])
    dictdata[moudle]=_dictdata
    return dictdata

Guess you like

Origin www.cnblogs.com/Testking/p/11809532.html