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

推荐订阅源

大猫的无限游戏
大猫的无限游戏
H
Hacker News: Front Page
T
The Blog of Author Tim Ferriss
WordPress大学
WordPress大学
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Blog — PlanetScale
Blog — PlanetScale
Stack Overflow Blog
Stack Overflow Blog
F
Fortinet All Blogs
H
Help Net Security
罗磊的独立博客
D
DataBreaches.Net
MyScale Blog
MyScale Blog
美团技术团队
人人都是产品经理
人人都是产品经理
L
LangChain Blog
M
MIT News - Artificial intelligence
C
Check Point Blog
GbyAI
GbyAI
B
Blog RSS Feed
Microsoft Azure Blog
Microsoft Azure Blog
Y
Y Combinator Blog
雷峰网
雷峰网
Last Week in AI
Last Week in AI
F
Full Disclosure
量子位
V
Visual Studio Blog
Google DeepMind News
Google DeepMind News
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
S
SegmentFault 最新的问题
云风的 BLOG
云风的 BLOG
H
Hackread – Cybersecurity News, Data Breaches, AI and More
P
Proofpoint News Feed
爱范儿
爱范儿
A
About on SuperTechFans
MongoDB | Blog
MongoDB | Blog
腾讯CDC
博客园 - 【当耐特】
U
Unit 42
Martin Fowler
Martin Fowler
NISL@THU
NISL@THU
B
Blog
T
The Exploit Database - CXSecurity.com
Apple Machine Learning Research
Apple Machine Learning Research
L
Lohrmann on Cybersecurity
P
Proofpoint News Feed
有赞技术团队
有赞技术团队
C
CERT Recently Published Vulnerability Notes
The GitHub Blog
The GitHub Blog
T
Threatpost

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