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

yipeiwu_com6年前Python基础

最近临时工作要生成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)

以上这篇对python 生成拼接xml报文的示例详解就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

python文件写入实例分析

本文实例讲述了python文件写入的用法。分享给大家供大家参考。具体分析如下: Python中wirte()方法把字符串写入文件,writelines()方法可以把列表中存储的内容写入文...

pytest中文文档之编写断言

编写断言 使用assert编写断言 pytest允许你使用python标准的assert表达式写断言; 例如,你可以这样做: # test_sample.py def func...

Python同步遍历多个列表的示例

Python同步遍历多个列表的示例

Python的for循环十分灵活,使用for循环我们可以很轻松地遍历一个列表,例如: a_list = ['z', 'c', 1, 5, 'm'] for each in a_lis...

OpenCV搞定腾讯滑块验证码的实现代码

OpenCV搞定腾讯滑块验证码的实现代码

前言 废话 滑块验证码破解是一直都想搞的项目,毕竟多数网站都会采用滑块验证码,于是最近在修改论文的闲暇之余把这事儿给解决了。要搞现在的滑块验证码绕不开图像处理,图像处理当然是首推Ope...

python读取文本中的坐标方法

利用python读取文本文件很方便,用到了string模块,下面用一个小例子演示读取文本中的坐标信息。 import string x , y , z = [] , [] ,[]...