[Python]快速解析資料庫檢視XML配置擷取資料庫欄位說明

來源:互聯網
上載者:User

在當前項目中,我收到資料庫開發人員提供的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 = pathdef addFile( self, filename ):self._files.append( filename )def getNodeValue( self, element, tagName ):return element.getElementsByTagName( tagName )[0].firstChild.nodeValuedef getSubNodeValue( self, element, tagName ):subNode = element.getElementsByTagName( 'BizObjPropertyDBInfo' )[0]return subNode.getElementsByTagName( tagName )[0].firstChild.nodeValuedef parseXml( self ):import xml.dom.minidomtry:for file in self._files:filename = self._path + '\\' + fileprint filenamef = open( filename )doc = xml.dom.minidom.parse( f )viewEName = doc.getElementsByTagName('BizObject')[0].getElementsByTagName('EName')[0].firstChild.nodeValueviewCName = doc.getElementsByTagName('BizObject')[0].getElementsByTagName('CName')[0].firstChild.nodeValueline = viewEName + ', , , , , , ' + viewCNameself._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 + ':' + Descriptionself._lines.append( line )finally:print "over"def printLines( self ):for line in self._lines:print linedef writeToCSVFile( self, outfilename ):import codecsfilename = self._path + '\\' + outfilenamef = codecs.open( filename,'w','utf-8' )for line in self._lines:f.write( line + '\n' )f.flush()f.close()# TestSuite ScriptsaObject = 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" )
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.