今天,我打算使用Python解析ssrsrdl文件(XML),以便收集数据集和查询数据。最近的一个项目让我回溯了各种报告和数据源,目的是整合和清理我们发布的内容。在
我可以使用此脚本创建一个包含以下列的CSV文件: 系统路径|报告文件名|命令类型|命令文本|
虽然不太雅致,但很管用。在
我希望通过这篇文章能够做的是,请那些已经尝试过或者在使用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()
我知道您要求使用Python;但我认为Powershell内置的xml处理功能会使这一点变得相当简单。虽然我确定它不是大师级的,但我认为它的效果相当不错(以#开头的行是注释):
相关问题 更多 >
编程相关推荐