1

今日は、DataSet と Query データを収集するために、Python を使用して SSRS RDL ファイル (XML) を解析することを目的として出発しました。最近のプロジェクトでは、公開したものを統合してクリーンアップする目的で、さまざまなレポートやデータ ソースを追跡しています。

このスクリプトを使用して、次の列を持つ CSV ファイルを作成できました: システム パス|レポート ファイル名|コマンド タイプ|コマンド テキスト|

あまりエレガントではありませんが、機能します。

この投稿で私ができることを望んでいるのは、すでにこれを試したことがあるか、Python を使用した XML 解析の経験がある専門家に、クリーンアップを試みて次の機能を提供してもらうことです。

  • XML タグであるヘッダーを含める
  • 列に DataSet 名を含める
  • 結果を単一のファイルに配信

「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()
4

1 に答える 1

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   
于 2010-07-10T21:25:21.387 に答える