python 实现xml文件转换成txt文档
xml文档:111.xml,下载网址:https://pan.baidu.com/s/18pcZWuYvRkZlEXYLcm9k_Apython文件:turn.py 下载网址:https://pan.baidu.com/s/1aqHuV69JDbZ-_CkFu3iM4w#!/usr/bin/evn python#coding: utf-8import os,shutilimp...
xml文档:111.xml,下载网址:https://pan.baidu.com/s/18pcZWuYvRkZlEXYLcm9k_A
python文件:turn.py 下载网址:https://pan.baidu.com/s/1aqHuV69JDbZ-_CkFu3iM4w
#!/usr/bin/evn python
#coding: utf-8
import os,shutil
import sys
import xml.dom.minidom
reload(sys)
sys.setdefaultencoding('utf8')
Xml='/root/file/111.xml'
Txt='/root/file/111.txt'
DOMTree = xml.dom.minidom.parse(Xml)
annotation = DOMTree.documentElement
objects = annotation.getElementsByTagName("DataGram")
f = open(Txt, "w")
for object in objects:
bbox = object.getElementsByTagName("Attachment")[0]
a1 = bbox.getElementsByTagName("AttachmentName")[0]
if a1.childNodes == []:
b1 = ''
else:
b1 = a1.childNodes[0].data
a2 = bbox.getElementsByTagName("AttachmentType")[0]
if a2.childNodes == []:
b2 = ''
else:
b2 = a2.childNodes[0].data
a3 = bbox.getElementsByTagName("AttachmentLocation")[0]
if a3.childNodes == []:
b3 = ''
else:
b3 = a3.childNodes[0].data
a4 = bbox.getElementsByTagName("AttachmentMessageType")[0]
if a4.childNodes == []:
b4 = ''
else:
b4 = a4.childNodes[0].data
a5 = bbox.getElementsByTagName("AttachmentMessageSchemaVersion")[0]
if a5.childNodes == []:
b5 = ''
else:
b5 = a5.childNodes[0].data
contents = b1+'|'+b2+'|'+b3+'|'+b4+'|'+b5+'\n'
f.write(contents)
f.close()
print '**********'+Xml+' -success stop***********'
执行完成后,点开111.txt文档
更多推荐
所有评论(0)