Python用ET包解析XML文件,写入到CSV中

废话不多说直接上代码,说明在注释中

#!/usr/bin/evn python 
#coding:utf-8 
import sys
reload(sys) 
sys.setdefaultencoding('utf-8') 
#上面4行处理utf-8字符编码

#引入PythonET包
try: 
    import xml.etree.cElementTree as ET 
except ImportError: 
    import xml.etree.ElementTree as ET 
import sys 

tableCount = 0

def getUmlClass(node, path):
    global tableCount   #使用全局变量
    target_tag = "UML.Class"
    path = path + '->' + node.tag   #取XML节点的tag
    if node.tag == target_tag:
        tableCount = tableCount + 1
        getUmlAttribute(node)
        print >> csvFile, ''    #将空行写入文件
    else:
        for child in node:  #子节点遍历
            getUmlClass(child, path)    #递归调用

def getUmlAttribute(nodeClass):
    tableName = nodeClass.attrib['name']    #取XML节点属性
    print >> csvFile,'TAB_INFO,'+tableName  ##将表名写入文件
    nodeFeature = nodeClass.find('UML.Classifier.feature')  #查找tag为UML.Classifier.feature的子节点
    if nodeFeature == None:
        return
    for nodeAttribute in nodeFeature.findall("UML.Attribute"):  #遍历所有tag为UML.Attribute的子节点
        columnName = nodeAttribute.attrib['name']
        columnType = 'Integer'
        columnLength = '0'
        columnPrecision = '0'
        columnScale = '0'
        columnDesc = 'empty'
        nodeColumnInfo = nodeAttribute.find('UML.ModelElement.taggedValue')
        for info in nodeColumnInfo.findall('UML.TaggedValue'):
            if info.attrib['tag'] == 'type':
                columnType = info.attrib['value']
            elif info.attrib['tag'] == 'length':
                columnLength = info.attrib['value']
            elif info.attrib['tag'] == 'precision':
                columnPrecision = info.attrib['value']
            elif info.attrib['tag'] == 'scale':
                columnScale = info.attrib['value']
            elif info.attrib['tag'] == 'description':
                columnDesc = info.attrib['value']
        print >> csvFile, 'COL_INFO,'+columnName+','+columnType ##将列信息写入文件

csvFile = open("output.csv", "w")   #打开文件写入
tableCount = 0
try: 
    tree = ET.parse("input.xml")     #打开xml文档 
    root = tree.getroot()         #获得root节点  
except Exception, e: 
    print "Error:cannot parse file:input.xml." 
    sys.exit(1) 
getUmlClass(root, "")
csvFile.close()
print "create",tableCount," tables from input.xml"

#print root[0][1].text   #通过下标访问 
#print root[0][1].attrib   #通过下标访问 
#print root[0].tag, root[0].text 
#
##修改xml文件 
#for country in root.findall('country'): 
#   rank = int(country.find('rank').text) 
#   if rank > 50: 
#       root.remove(country) 
#
#tree.write('output.xml') 
  • 2
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

皓月如我

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值