2010-07-08 15 views
1

我今天提出了使用Python解析SSRS RDL文件(XML)以收集DataSet和Query數據的意圖。最近的一個項目讓我回顧了各種報告和數據源,旨在整合和清理我們發佈的內容。使用Python來抓取RDL中的數據集和查詢數據

我能夠使用此腳本創建CSV文件,其中包含以下列: 系統路徑|報告文件名|命令類型|命令文本|

這不是很優雅,但它的工作原理。

我希望能夠用這篇文章做的事情是爲你們任何一位已經嘗試過這方面的專家徵求意見,或者在使用Python進行XML解析方面經驗豐富,以幫助他們清理並提供幫助能力:

  • 包括頁眉,這將是XML標籤
  • 列包含數據集名稱
  • 交付成果轉化爲單個文件

這裏是我的「rdlparser.py」文件中的完整代碼:

import sys, os 

from xml.dom import minidom 
xmldoc = minidom.parse(sys.argv[1]) 

content = "" 
TargetFile = sys.argv[1].split(".", 1)[0] + ".csv" 
numberOfQueryNodes = 0 

queryNodes = xmldoc.getElementsByTagName('Query') 
numberOfQueryNodes = queryNodes.length -1 


while (numberOfQueryNodes > -1): 
    content = content + os.path.abspath(sys.argv[1])+ '|'+ sys.argv[1].split(".", 1)[0]+ '|' 
    outputNode = queryNodes.__getitem__(numberOfQueryNodes) 
    children = [child for child in outputNode.childNodes if child.nodeType==1] 
    numberOfQueryNodes = numberOfQueryNodes - 1 
    for node in children: 
     if node.firstChild.nodeValue != '\n   ': 
      if node.firstChild.nodeValue != 'true': 
       content = content + node.firstChild.nodeValue + '|' 
    content = content + '\n' 

fp = open(TargetFile, 'wb') 
fp.write(content) 
fp.close() 

回答

0

我知道你問了Python;但我想Powershell的內置xml處理功能會讓這個過程變得非常簡單。雖然我敢肯定,這是不是大師的水平,我覺得它出來很漂亮(以#開始的行是註釋):

# The directory to search 
$searchpath = "C:\" 

# List all rdl files from the given search path recusrivley searching sub folders, store results into a variable 
$files = gci $searchpath -recurse -filter "*.rdl" | SELECT FullName, DirectoryName, Name 

# for each of the found files pass the folder and file name and the xml content 
$files | % {$Directory = $_.DirectoryName; $Name = $_.Name; [xml](gc $_.FullName)} 
      # in the xml content navigate to the the DataSets Element 
      | % {$_.Report.DataSets} 
        # for each query retrieve the Report directory , File Name, DataSource Name, Command Type, Command Text output thwese to a csv file 
        | % {$_.DataSet.Query} | SELECT @{N="Path";E={$Directory}}, @{N="File";E={$Name}}, DataSourceName, CommandType, CommandText | Export-Csv Test.csv -notype 
+0

一點點改變精美努力得到它到.ps1文件。我把它扔進去並試圖運行它,而powershell不喜歡「空管元素」,所以我把最後的$ files命令放在同一行上。 我沒有太多曝光PowerShell,但這只是讓我進入它的東西。感謝JasonHorner的幫助! – Vinnie 2010-07-12 14:58:14