将xml 文件转化为txt文件

# !/usr/bin/evn python
# coding: utf-8

import os, shutil
import sys
import xml.dom.minidom
import importlib
importlib.reload(sys)



Xml = 'D:/Downloads/222.xml'
Txt = 'D:/Downloads/222.txt'

DOMTree = xml.dom.minidom.parse(Xml)
annotation = DOMTree.documentElement
objects = annotation.getElementsByTagName("Attachment")
f = open(Txt, "a")

for object in objects:
    bbox = object
    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 + '\n')
f.close()


你可能感兴趣的:(将xml 文件转化为txt文件)