惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

SecWiki News
SecWiki News
N
Netflix TechBlog - Medium
D
Docker
Stack Overflow Blog
Stack Overflow Blog
云风的 BLOG
云风的 BLOG
U
Unit 42
Recorded Future
Recorded Future
G
Google Developers Blog
T
Threatpost
Google DeepMind News
Google DeepMind News
D
DataBreaches.Net
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
T
The Blog of Author Tim Ferriss
C
Cyber Attacks, Cyber Crime and Cyber Security
A
Arctic Wolf
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
GbyAI
GbyAI
V
Vulnerabilities – Threatpost
Project Zero
Project Zero
WordPress大学
WordPress大学
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
M
MIT News - Artificial intelligence
月光博客
月光博客
Spread Privacy
Spread Privacy
Know Your Adversary
Know Your Adversary
人人都是产品经理
人人都是产品经理
D
Darknet – Hacking Tools, Hacker News & Cyber Security
The Hacker News
The Hacker News
K
Kaspersky official blog
Simon Willison's Weblog
Simon Willison's Weblog
MongoDB | Blog
MongoDB | Blog
J
Java Code Geeks
I
Intezer
Y
Y Combinator Blog
P
Proofpoint News Feed
G
GRAHAM CLULEY
L
LINUX DO - 热门话题
Schneier on Security
Schneier on Security
B
Blog
Jina AI
Jina AI
V2EX - 技术
V2EX - 技术
雷峰网
雷峰网
Last Week in AI
Last Week in AI
V
V2EX
Martin Fowler
Martin Fowler
P
Palo Alto Networks Blog
Latest news
Latest news
Google DeepMind News
Google DeepMind News
L
Lohrmann on Cybersecurity
The Last Watchdog
The Last Watchdog

博客园 - yankchina

[C++]VisualC++2010Express英文版试用手记 [C++]C++学习的一些建议 [Soft]物联网时代中的编程语言 - yankchina - 博客园 [Web]Web在线地图API试用笔记 [Python]命令行解析器Argparse的试用手册 [PHP]PHP技术基础 [Blog]将syntaxhighlight与LatexMathML本地化 [Soft]软件技术的两个趋势 [Soft]RAD的代价 用VBA批量输出Outlook通讯录内容 - yankchina - 博客园 试用交互原型设计软件Axure RP Pro 4 云计算零接触 Camtasia Studio 7 试用笔记 文档整理经验谈 命令行调用Lame批量压缩MP3 [C++]千万不要碰VisualStudio - yankchina - 博客园 CSS 使用经验备忘 用Compare It! 4 比较Word Wiki 比较
[Python]快速解析数据库视图XML配置获取数据库字段说明
yankchina · 2010-05-17 · via 博客园 - yankchina

在当前项目中,我收到数据库开发人员提供的XML视图文件,其中包含了表信息; 但这些信息混杂在大量的UI配置中,很难阅读,于是我决定用Python来编写一个简单的程序来进行 XML 解析,将所需的数据字段信息转换成CSV格式,再导入到Excel中(耗时2小时),有如下几点技术体会:

  1. Python中采用minidom进行解析时,其XML文件必须是UTF-8编码格式,否则会出错。在进行解析前要先进行编码转换工作;
  2. Python中的DOM节点Node值获取必须要用firstChild.nodeValue形式,不能直接用nodeValue来获取;
  3. Python中解析后的String值都是UTF-8格式,所以其File IO操作必须用codecs方式;
  4. Python编程时逐步从逐行解释方式过渡到OPP方式,这样虽然步骤比较多,但调试方便;

参考代码如下:

class dbviewxmladapter:
	"""
	"""

	def __init__(self):
		self._version = "0.1"
		self._path = "e:\\Temp\\Work"
		self._files = []
		self._lines = []

	def setPath( self, path ):
		self._path = path

	def addFile( self, filename ):
		self._files.append( filename )

	def getNodeValue( self, element, tagName ):
		return element.getElementsByTagName( tagName )[0].firstChild.nodeValue

	def getSubNodeValue( self, element, tagName ):
		subNode = element.getElementsByTagName( 'BizObjPropertyDBInfo' )[0]
		return subNode.getElementsByTagName( tagName )[0].firstChild.nodeValue

	def parseXml( self ):
		import xml.dom.minidom
		try:
			for file in self._files:
				filename = self._path + '\\' + file
				print filename
				f = open( filename )
				doc = xml.dom.minidom.parse( f )
				viewEName = doc.getElementsByTagName('BizObject')[0].getElementsByTagName('EName')[0].firstChild.nodeValue
				viewCName = doc.getElementsByTagName('BizObject')[0].getElementsByTagName('CName')[0].firstChild.nodeValue
				line = viewEName + ', , , , , , ' + viewCName
				self._lines.append( line )
				items = doc.getElementsByTagName( 'BizObjProperty' )
				for item in items:
					EName = self.getNodeValue( item, 'EName' )
					CName = self.getNodeValue( item, 'CName' )
					Description = self.getNodeValue( item, 'Description' )
					Type = self.getSubNodeValue( item, 'Type' )
					Length = self.getSubNodeValue( item, 'Length' )
					Size = self.getSubNodeValue( item, 'Size')
					IsPK = self.getSubNodeValue( item, 'IsPK' ) == '1'
					IsNullable = self.getSubNodeValue( item, 'IsNullable' ) == '1'
					line = EName + ',' + Type + ',' + Length + ',' + Size + ',' + str(IsPK) + ', ' + str(IsNullable) + ',' + CName + ':' + Description
					self._lines.append( line )
		finally:
			print "over"

	def printLines( self ):
		for line in self._lines:
			print line
			
	def writeToCSVFile( self, outfilename ):
		import codecs
		filename = self._path + '\\' + outfilename
		f = codecs.open( filename,'w','utf-8' )
		for line in self._lines:
			f.write( line + '\n' )
		f.flush()
		f.close()

# TestSuite Scripts
aObject = dbviewxmladapter()
for i in range(5):
	filename = str(i+1) + ".xml"
	aObject.addFile( filename )
#aObject.addFile("5.xml")
aObject.parseXml()
#aObject.printLines()
aObject.writeToCSVFile( "all.csv" )