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

推荐订阅源

Apple Machine Learning Research
Apple Machine Learning Research
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
S
Security @ Cisco Blogs
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Cisco Talos Blog
Cisco Talos Blog
Cyberwarzone
Cyberwarzone
SecWiki News
SecWiki News
Webroot Blog
Webroot Blog
L
LINUX DO - 最新话题
V
Vulnerabilities – Threatpost
T
Troy Hunt's Blog
Cloudbric
Cloudbric
L
LINUX DO - 热门话题
Google DeepMind News
Google DeepMind News
H
Heimdal Security Blog
S
Schneier on Security
NISL@THU
NISL@THU
The Hacker News
The Hacker News
Attack and Defense Labs
Attack and Defense Labs
A
Arctic Wolf
V2EX - 技术
V2EX - 技术
Security Latest
Security Latest
AWS News Blog
AWS News Blog
Scott Helme
Scott Helme
W
WeLiveSecurity
S
Secure Thoughts
Y
Y Combinator Blog
GbyAI
GbyAI
H
Hackread – Cybersecurity News, Data Breaches, AI and More
博客园 - Franky
量子位
人人都是产品经理
人人都是产品经理
雷峰网
雷峰网
K
Kaspersky official blog
P
Privacy & Cybersecurity Law Blog
T
Tenable Blog
The GitHub Blog
The GitHub Blog
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
J
Java Code Geeks
Vercel News
Vercel News
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Schneier on Security
Schneier on Security
云风的 BLOG
云风的 BLOG
小众软件
小众软件
Engineering at Meta
Engineering at Meta
宝玉的分享
宝玉的分享
C
CERT Recently Published Vulnerability Notes
Security Archives - TechRepublic
Security Archives - TechRepublic
C
CXSECURITY Database RSS Feed - CXSecurity.com
P
Palo Alto Networks Blog

博客园 - 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" )