本次說明生成乙個xml格式的報文,並將其寫入文字
# -*- coding:utf8 -*- # 避免中文亂碼
__author__ = 'lxf'
import xml.dom.minidom as dom
from xml.dom import minidom
import random
import time
# 這個方法用來代替minidom裡格式化**,實現節點不換行
def fixed_writexml(self, writer, indent="", addindent="", newl=""):
# indent = current indentation
# addindent = indentation to add to higher levels
# newl = newline string
writer.write(indent + "<" + self.tagname)
attrs = self._get_attributes()
a_names = attrs.keys()
a_names.sort()
for a_name in a_names:
writer.write(" %s=\"" % a_name)
minidom._write_data(writer, attrs[a_name].value)
writer.write("\"")
if self.childnodes:
if len(self.childnodes) == 1 \
and self.childnodes[0].nodetype == minidom.node.text_node:
writer.write(">")
self.childnodes[0].writexml(writer, "", "", "")
writer.write("%s" % (self.tagname, newl))
return
writer.write(">%s" % (newl))
for node in self.childnodes:
if node.nodetype is not minidom.node.text_node:
node.writexml(writer, indent + addindent, addindent, newl)
writer.write("%s%s" % (indent, self.tagname, newl))
else:
writer.write("/>%s" % (newl))
minidom.element.writexml = fixed_writexml
def write():
doc = dom.document()
root_node = doc.createelement("service")
head = doc.createelement("sys_head")
body = doc.createelement("body")
# service_code
service_node = doc.createelement("service_code")
service_value = doc.createtextnode("11005000004")
service_node.setattribute("attr", "field")
# service_scene
scene_node = doc.createelement("service_scene")
scene_value = doc.createtextnode("03")
scene_node.setattribute("attr", "field")
# consumer_id
consumer_node = doc.createelement("consumer_id")
consumer_value = doc.createtextnode("033500")
consumer_node.setattribute("attr", "field")
# consumer_seq_no
seq_node = doc.createelement("consumer_seq_no")
seq = 133100201803220002704750 + random.randint(1, 10000)
seq_value = doc.createtextnode('%d' % seq)
seq_node.setattribute("attr", "field")
# buss_seq_no
bus_node = doc.createelement("buss_seq_no")
bus_value = doc.createtextnode("2018032284584195")
bus_node.setattribute("attr", "field")
# tran_flag
flag_node = doc.createelement("tran_flag")
flag_value = doc.createtextnode("0")
flag_node.setattribute("attr", "field")
# tran_date
date_node = doc.createelement("tran_date")
date_value = doc.createtextnode(time.strftime('%y%m%d'))
date_node.setattribute("attr", "field")
# tran_timestamp
time_node = doc.createelement("tran_timestamp")
time_value = doc.createtextnode("000014")
time_node.setattribute("attr", "field")
c_node = doc.createelement("end_date")
c_value = doc.createtextnode("20180322")
c_node.setattribute("attr", "field")
print (doc.toxml("utf-8"))
f = open("ttt.xml", "w")
f.write(doc.toprettyxml(indent="\t", newl="\n", encoding="utf-8"))
f.close()
if __name__ == "__main__":
write()
下面是生成的xml報文
<?xml version="1.0" encoding="utf-8"?>
11005000004
03033500
133100201803220002704951
2018032284584195
0 20180523
000014
20180322
python 生成xml檔案
from xml.dom.minidom import document 確保caffe在python路徑 import xml.dom.minidom doc document 建立dom文件物件 object doc.createelement objects objec name person...
Python生成XML檔案
import xml.dom.minidom 在記憶體中建立乙個空的文件 doc xml.dom.minidom.document 建立乙個根節點managers物件 root doc.createelement managers 設定根節點的屬性 root.setattribute company...
python生成xml檔案
先上 1 usr bin env python32 coding utf 8 3from xml.dom.minidom import document67 defreadfile filename,lines 8 with open filename,r as f 9for line in f 1...