format xml

前端之家收集整理的这篇文章主要介绍了format xml前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
#-------------------------------------------------------------------------------
# Name:        fxml.py
# Purpose:
#
# Author:      grail
#
# Created:     20-04-2013
# Copyright:   (c) grail 2013
# Licence:     <your licence>
#-------------------------------------------------------------------------------

import os
import sys
from xml.dom.minidom import parse,parseString

def formatXML(folder,pretty):
    xmlPathList = os.listdir(folder)
    for xmlPath in xmlPathList:
        if (xmlPath[-4:] == ".xml"):
            xmlPathName = os.path.join(folder,xmlPath)
            print xmlPathName
            xmldom = parse(xmlPathName)
            xmlstring = xmldom.toxml("UTF-8")
            linnerxmlstring = ""
            for xmlline in xmlstring.splitlines():
                linnerxmlstring = linnerxmlstring + xmlline.strip()
            gslFile = None
            gslFile = open(xmlPathName,"wb")
            if (pretty):
                gslFile.write(parseString(linnerxmlstring).toprettyxml("  ","\n","UTF-8"))
            else:
                gslFile.write(linnerxmlstring)
                gslFile.write("\n")
            gslFile.write("\n")
            gslFile.close()
    pass

def main():
    print sys.argv
    folder = r"E:\04_chrome_workspace\IAC-407\scripts"
##    formatXML(folder,True)
    formatXML(folder,False)
    pass

if __name__ == '__main__':
    main()

原文链接:https://www.f2er.com/xml/300580.html

猜你在找的XML相关文章