对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设计】。

相关文章

tensorflow构建BP神经网络的方法

之前的一篇博客专门介绍了神经网络的搭建,是在python环境下基于numpy搭建的,之前的numpy版两层神经网络,不能支持增加神经网络的层数。最近看了一个介绍tensorflow的视频...

在Python中os.fork()产生子进程的例子

例1 import os print 'Process (%s) start...' %os.getpid() pid = os.fork() if pid==0: print...

win10子系统python开发环境准备及kenlm和nltk的使用教程

前言 因为NLP作业需要用到kenlm,而kenlm在linux下更为方便。本人win10之前开启了子系统,所以就打算在子系统下进行相关作业的完成。 首先开启win10子系统,网上教...

Python实现的ini文件操作类分享

类代码: # -*- coding:gbk -*- import ConfigParser, os class INIFILE: def __init__(self, filen...

python:socket传输大文件示例

文件可以传输,但是对比传输前后的文件:socket_test.txt,末尾有一些不一致服务端代码: #!/usr/bin/python # -*- coding: utf-8 -*-...