对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用fsolve、leastsq对非线性方程组求解

背景: 实现用python的optimize库的fsolve对非线性方程组进行求解。可以看到这一个问题实际上还是一个优化问题,也可以用之前拟合函数的leastsq求解。下面用这两个方法进...

python编写弹球游戏的实现代码

 弹球游戏: from tkinter import * import time import random tk=Tk() #创建一个界面 tk...

python时间日期函数与利用pandas进行时间序列处理详解

python标准库包含于日期(date)和时间(time)数据的数据类型,datetime、time以及calendar模块会被经常用到。 datetime以毫秒形式存储日期和时间,da...

python 多线程串行和并行的实例

如下所示: #coding=utf-8 import threading import time import cx_Oracle from pprint import pprint...

对Python闭包与延迟绑定的方法详解

Python闭包可能会在面试或者是工作中经常碰到,而提到Python的延迟绑定,肯定就离不开闭包的理解,今天总结下 关于闭包的概念以及一个延迟绑定的面试题。 Python闭包 1、什么是...