xml与json格式互转

最近要整一些报文测试的事情,可当前项目的请求报文格式却不统一,有XML也有JSON,为了一致性,决定统一用JSON格式处理。

 xmltodict : Makes working with XML feel like you are working with JSON

 

1.xml转json

import xmltodict
import json

dic = xmltodict.parse("""<mydocument has="an attribute">
<and>
<many>elements</many>
<many>more elements</many>
</and>
<plus a="complex">
element as well
</plus>
</mydocument>""")

dic1 = xmltodict.parse("""<?xml version="1.0" encoding="UTF-8"?>
<root>
<object name="input">
<field name="credit_ccy_code" value="USD"/>
<field name="credit_acct_no" value="991200001030"/>
<field name="credit_acct_name" value="Test_PA"/>
<field name="base_exch_rate" value=""/>
<field name="cheque_acct_name" value=""/>
<field name="summary_code" value=""/>
<field name="forward_cheque_ind" value=""/>
<field name="elec_cheque_ind" value=""/>
<field name="cheque_amt" value=""/>
<field name="cheque_ccy" value=""/>
<field name="trxn_date" value="20170110"/>
<field name="cheque_bank_code" value=""/>
<field name="customer_remark" value=""/>
<field name="commercial_bank_name" value=""/>
<field name="exch_rate" value=""/>
<field name="forex_agree_price_id" value=""/>
<field name="start_inst_date" value=""/>
<field name="cheque_no" value=""/>
<field name="trxn_remark" value=""/>

<array name="list01" length="1">
<object name="">
<field name="base_exch_rate" value="0.00000"/>
<field name="cheque_acct_name" value=""/>
<field name="summary_code" value="CSD"/>
<field name="forward_cheque_ind" value="N"/>
<field name="elec_cheque_ind" value="N"/>
<field name="cheque_amt" value="1000.00"/>
<field name="cheque_bank_code" value="BOC_MAC"/>
<field name="cheque_ccy" value="USD"/>
<field name="id" value="1"/>
<field name="customer_remark" value=""/>
<field name="commercial_bank_name" value=""/>
<field name="exch_rate" value="0.00000"/>
<field name="forex_agree_price_id" value=""/>
<field name="start_inst_date" value="20170110"/>
<field name="cheque_no" value="ZSBANK20170001"/>
<field name="trxn_remark" value=""/>
<field name="cheque_acct_no" value=""/>
<field name="collection_check_type" value="FC"/>
</object></array>

<field name="cheque_acct_no" value=""/>
<field name="collection_check_type" value=""/>
</object>

<object name="comm_req">
<field name="auto_chrg_info" value=""/>
<field name="page_size" value="20"/>
<field name="busi_seq" value=""/>
<field name="session_id" value=""/>
<field name="agent_org_id" value=""/>
<field name="recon_code" value=""/>
<field name="busi_org_id" value="99"/>
<field name="total_count" value=""/>
<field name="channel_id" value="101"/>
<field name="initiator_date" value="20170110"/>
<field name="page_start" value="0"/>
<field name="busi_branch_id" value="99101"/>
<field name="chrg_confirm_ind" value=""/>
<field name="over_time" value=""/>
<field name="ip_address" value="10.18.21.27"/>
<field name="expect_date" value="20170110"/>
<field name="initiator_system" value="100"/>
<field name="role_collection" value="ROLE0004,ROLE0007,ROLE0010,@99101"/>
<field name="busi_teller_id" value="99101888"/>
<field name="sponsor_system" value="100"/>
<array name="auth_reason" length="0"/>
<field name="auth_ind" value=""/>
<field name="auth_user_id" value=""/>
<field name="branch_id" value="99101"/>
</object>

<object name="sys">
<field name="prcscd" value="20211"/>
<field name="bizSysSrvId" value=""/>
</object>
</root>""")

print (json.dumps(dic,indent=4))
print '----------'
print (json.dumps(dic1))

输出:

{
    "mydocument": {
        "@has": "an attribute", 
        "and": {
            "many": [
                "elements", 
                "more elements"
            ]
        }, 
        "plus": {
            "@a": "complex", 
            "#text": "element as well"
        }
    }
}
-------
{"root": {"object": [{"@name": "input", "field": [{"@name": "credit_ccy_code", "@value": "USD"}, {"@name": "credit_acct_no", "@value": "991200001030"}, {"@name": "credit_acct_name", "@value": "Test_PA"}, {"@name": "base_exch_rate", "@value": ""}, {"@name": "cheque_acct_name", "@value": ""}, {"@name": "summary_code", "@value": ""}, {"@name": "forward_cheque_ind", "@value": ""}, {"@name": "elec_cheque_ind", "@value": ""}, {"@name": "cheque_amt", "@value": ""}, {"@name": "cheque_ccy", "@value": ""}, {"@name": "trxn_date", "@value": "20170110"}, {"@name": "cheque_bank_code", "@value": ""}, {"@name": "customer_remark", "@value": ""}, {"@name": "commercial_bank_name", "@value": ""}, {"@name": "exch_rate", "@value": ""}, {"@name": "forex_agree_price_id", "@value": ""}, {"@name": "start_inst_date", "@value": ""}, {"@name": "cheque_no", "@value": ""}, {"@name": "trxn_remark", "@value": ""}, {"@name": "cheque_acct_no", "@value": ""}, {"@name": "collection_check_type", "@value": ""}], "array": {"@name": "list01", "@length": "1", "object": {"@name": "", "field": [{"@name": "base_exch_rate", "@value": "0.00000"}, {"@name": "cheque_acct_name", "@value": ""}, {"@name": "summary_code", "@value": "CSD"}, {"@name": "forward_cheque_ind", "@value": "N"}, {"@name": "elec_cheque_ind", "@value": "N"}, {"@name": "cheque_amt", "@value": "1000.00"}, {"@name": "cheque_bank_code", "@value": "BOC_MAC"}, {"@name": "cheque_ccy", "@value": "USD"}, {"@name": "id", "@value": "1"}, {"@name": "customer_remark", "@value": ""}, {"@name": "commercial_bank_name", "@value": ""}, {"@name": "exch_rate", "@value": "0.00000"}, {"@name": "forex_agree_price_id", "@value": ""}, {"@name": "start_inst_date", "@value": "20170110"}, {"@name": "cheque_no", "@value": "ZSBANK20170001"}, {"@name": "trxn_remark", "@value": ""}, {"@name": "cheque_acct_no", "@value": ""}, {"@name": "collection_check_type", "@value": "FC"}]}}}, {"@name": "comm_req", "field": [{"@name": "auto_chrg_info", "@value": ""}, {"@name": "page_size", "@value": "20"}, {"@name": "busi_seq", "@value": ""}, {"@name": "session_id", "@value": ""}, {"@name": "agent_org_id", "@value": ""}, {"@name": "recon_code", "@value": ""}, {"@name": "busi_org_id", "@value": "99"}, {"@name": "total_count", "@value": ""}, {"@name": "channel_id", "@value": "101"}, {"@name": "initiator_date", "@value": "20170110"}, {"@name": "page_start", "@value": "0"}, {"@name": "busi_branch_id", "@value": "99101"}, {"@name": "chrg_confirm_ind", "@value": ""}, {"@name": "over_time", "@value": ""}, {"@name": "ip_address", "@value": "10.18.21.27"}, {"@name": "expect_date", "@value": "20170110"}, {"@name": "initiator_system", "@value": "100"}, {"@name": "role_collection", "@value": "ROLE0004,ROLE0007,ROLE0010,@99101"}, {"@name": "busi_teller_id", "@value": "99101888"}, {"@name": "sponsor_system", "@value": "100"}, {"@name": "auth_ind", "@value": ""}, {"@name": "auth_user_id", "@value": ""}, {"@name": "branch_id", "@value": "99101"}], "array": {"@name": "auth_reason", "@length": "0"}}, {"@name": "sys", "field": [{"@name": "prcscd", "@value": "20211"}, {"@name": "bizSysSrvId", "@value": ""}]}]}}

 

 2.json转xml

import xmltodict

dicts = {
"sys":{
"prcscd":"2211",
"error_id":1
},
"input":{
"list01":[],
"credit_ccy_code":1,
"credit_acct_no":1,
"credit_acct_name":1,
"credit_acct_type":1,
"credit_prod_id":1,
"act_dept_amt":1,
"passbook_no":1,
"passbook_balance":1
},
"comm_req":{
"busi_org_id":1,
"channel_id":1,
"trxn_seq":1,
"busi_seq":1,
"sponsor_system":1,
"initiator_system":1,
"initiator_date":1,
"recon_code":1,
"busi_teller_id":1,
"busi_branch_id":1,
"expect_date":1,
"agent_org_id":1,
"session_id":1,
"ip_address":1,
"over_time":1,
"page_size":1,
"page_start":1,
"total_count":1,
"auth_ind":1,
"auth_user_id":1,
"auto_chrg_method":1,
"chrg_confirm_ind":1,
"auto_chrg_info":{
"semi_auto_chrg_ind":1,
"cash_trxn_ind":1,
"chrg_asso_obj_type":1,
"cust_no":1,
"acct_no":1,
"ccy_code":1,
"trxn_amt":1,
"quantity":1,
"deduct_chrg_acct":1,
"deduct_chrg_ccy":1,
"deduct_sub_acct_seq":1,
"summary_code":1,
"paid_ind":1,
"charge_details":[]
},
"role_collection":1,
"external_scene_code":1,
"warning_confirm_ind":1,
"ince_id":1
}
}

dicts = {'root':dicts}   #若没有会报错 Document must have exactly one root
print (xmltodict.unparse(dicts,pretty = True))

输出:

<?xml version="1.0" encoding="utf-8"?>
<root>
    <sys>
        <error_id>1</error_id>
        <prcscd>2211</prcscd>
    </sys>
    <input>
        <passbook_no>1</passbook_no>
        <credit_acct_type>1</credit_acct_type>
        <act_dept_amt>1</act_dept_amt>
        <passbook_balance>1</passbook_balance>
        <credit_acct_name>1</credit_acct_name>
        <credit_ccy_code>1</credit_ccy_code>
        <credit_acct_no>1</credit_acct_no>
        <credit_prod_id>1</credit_prod_id>
    </input>
    <comm_req>
        <initiator_date>1</initiator_date>
        <trxn_seq>1</trxn_seq>
        <busi_org_id>1</busi_org_id>
        <sponsor_system>1</sponsor_system>
        <warning_confirm_ind>1</warning_confirm_ind>
        <chrg_confirm_ind>1</chrg_confirm_ind>
        <auth_ind>1</auth_ind>
        <role_collection>1</role_collection>
        <auto_chrg_info>
            <summary_code>1</summary_code>
            <cust_no>1</cust_no>
            <ccy_code>1</ccy_code>
            <acct_no>1</acct_no>
            <deduct_chrg_ccy>1</deduct_chrg_ccy>
            <deduct_sub_acct_seq>1</deduct_sub_acct_seq>
            <chrg_asso_obj_type>1</chrg_asso_obj_type>
            <trxn_amt>1</trxn_amt>
            <deduct_chrg_acct>1</deduct_chrg_acct>
            <semi_auto_chrg_ind>1</semi_auto_chrg_ind>
            <paid_ind>1</paid_ind>
            <cash_trxn_ind>1</cash_trxn_ind>
            <quantity>1</quantity>
        </auto_chrg_info>
        <page_size>1</page_size>
        <agent_org_id>1</agent_org_id>
        <expect_date>1</expect_date>
        <page_start>1</page_start>
        <ince_id>1</ince_id>
        <initiator_system>1</initiator_system>
        <over_time>1</over_time>
        <busi_branch_id>1</busi_branch_id>
        <external_scene_code>1</external_scene_code>
        <busi_teller_id>1</busi_teller_id>
        <ip_address>1</ip_address>
        <auth_user_id>1</auth_user_id>
        <recon_code>1</recon_code>
        <total_count>1</total_count>
        <auto_chrg_method>1</auto_chrg_method>
        <session_id>1</session_id>
        <busi_seq>1</busi_seq>
        <channel_id>1</channel_id>
    </comm_req>
</root>

 

posted on 2017-12-11 17:28  Ryana  阅读(2355)  评论(0编辑  收藏  举报