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

推荐订阅源

H
Hacker News: Front Page
博客园 - 【当耐特】
量子位
博客园 - 聂微东
H
Hackread – Cybersecurity News, Data Breaches, AI and More
The Register - Security
The Register - Security
Last Week in AI
Last Week in AI
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
T
Tailwind CSS Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
雷峰网
雷峰网
Blog — PlanetScale
Blog — PlanetScale
P
Proofpoint News Feed
Jina AI
Jina AI
The Cloudflare Blog
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
AWS News Blog
AWS News Blog
L
LINUX DO - 最新话题
酷 壳 – CoolShell
酷 壳 – CoolShell
The GitHub Blog
The GitHub Blog
B
Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
S
Security @ Cisco Blogs
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
S
Schneier on Security
Stack Overflow Blog
Stack Overflow Blog
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Engineering at Meta
Engineering at Meta
W
WeLiveSecurity
博客园 - 三生石上(FineUI控件)
Security Archives - TechRepublic
Security Archives - TechRepublic
Hugging Face - Blog
Hugging Face - Blog
T
Troy Hunt's Blog
C
CERT Recently Published Vulnerability Notes
N
News and Events Feed by Topic
S
SegmentFault 最新的问题
美团技术团队
C
CXSECURITY Database RSS Feed - CXSecurity.com
人人都是产品经理
人人都是产品经理
SecWiki News
SecWiki News
N
News and Events Feed by Topic
C
Check Point Blog
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
宝玉的分享
宝玉的分享
Schneier on Security
Schneier on Security
阮一峰的网络日志
阮一峰的网络日志
Cisco Talos Blog
Cisco Talos Blog
T
Threat Research - Cisco Blogs
J
Java Code Geeks

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