网络编程
位置:首页>> 网络编程>> Python编程>> 对python 生成拼接xml报文的示例详解

对python 生成拼接xml报文的示例详解

作者:meiguopai1  发布时间:2021-10-24 13:07:38 

标签:python,拼接,xml,报文

最近临时工作要生成xml报名,通过MQ接口发送。简单小程序。

自增长拼成xml报文

Test_001.py


# encoding=utf-8
import time

orderId = ''
s1= "\n"
#
for ID in range(1,5):
 item1 = "<item>" + \
     "<orderID>" + str(ID) + "</orderID>" + \
     "<time>" + time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()) + "</time>" + \
     "</item>"

orderId+=item1
messge = "<MbfBody>" + orderId + "</MbfBody> "
print(messge)

另外一种状态


#encoding=utf-8

# str=input("输入字段:")
str='lxs,hqq,lj,xc'
List=str.split(',')

# str_xml=input("输入替换的模板:")
str_xml='<step id="xml_set_xml_value" comment="value" isrun="true"><param id="xml">VAR_XML</param><param id="xpath">//MbfBody/value</param><param id="value">COLUMN(VALUE,y)</param></step>'

#列表追加,回车成多行
def add_xml(L):
 xml_list=[]
 s1= '\n' #回车换行符
 for value in L:
   VAULE=value.upper()
   xml= str_xml.replace('value',value,2).replace('VALUE',VAULE,1) #替换模板中的值为列表中的值,小写两次,大写一次
   xml_list.append(xml)
 xml_str=s1.join(xml_list) #list 更新成str
 return xml_str

#字符串追加,一行
# def add_xml(L):
#   xml_list=''
#   for value in L:
#     VAULE=value.upper()
#     xml= str_xml.replace('value',value,2).replace('VALUE',VAULE,1) #替换模板中的值为列表中的值,小写两次,大写一次
#     xml_list+=xml
#   # xml_str=s1.join(xml_list) #list 更新成str
#   return xml_list

test=add_xml(List)
print(test)

来源:https://blog.csdn.net/meiguopai1/article/details/73610728

0
投稿

猜你喜欢

手机版 网络编程 asp之家 www.aspxhome.com