我今天开始着手使用Python解析SSRS RDL文件(XML)以收集DataSet和Query数据。最近的一个项目让我回顾了各种报告和数据来源,目的是整合和清理我们发布的内容。
我能够使用此脚本创建包含以下列的CSV文件: system path | report file name | command type | command text |
它不是很优雅,但它有效。
我希望能够对这篇文章做些什么,请求任何一位已经尝试过这方面的人或者使用Python进行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 :(得分:0)
我知道你问过Python;但我认为Powershell内置的xml处理功能会让这个变得相当简单。虽然我确定它不是guru级别,但我认为它的表现相当不错(以#开头的行是注释):
# 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