我使用Python的iterparse
来解析nessus扫描的XML结果(.nessus文件)。解析意外记录时失败,因为已正确分析了类似的记录。在
XML文件的一般结构是许多记录,如下所示:
<ReportHost>
<ReportItem>
<foo>9.3</foo>
<bar>hello</bar>
</ReportItem>
<ReportItem>
<foo>10.0</foo>
<bar>world</bar>
</ReportHost>
<ReportHost>
...
</ReportHost>
换句话说,许多主机(ReportHost
)有很多要报告的项目(ReportItem
),后者有几个特征(foo
,bar
)。我将考虑为每个项目生成一行,以及它的特性。在
解析在文件中间的一行中失败(foo
,在这种情况下是cvss_base_score
)
而大约200条类似的行已经被解析,没有问题。在
相关的代码如下——它设置了上下文标记(inReportHost
和{
import xml.etree.cElementTree as ET
inReportHost = False
inReportItem = False
for event, elem in ET.iterparse("test2.nessus", events=("start", "end")):
if event == 'start' and elem.tag == "ReportHost":
inReportHost = True
if event == 'end' and elem.tag == "ReportHost":
inReportHost = False
elem.clear()
if inReportHost:
if event == 'start' and elem.tag == 'ReportItem':
inReportItem = True
cvss = ''
if event == 'start' and inReportItem:
if event == 'start' and elem.tag == 'cvss_base_score':
cvss = elem.text
if event == 'end' and elem.tag == 'ReportItem':
print cvss
inReportItem = False
cvss
有时有None值(在cvss = elem.text
赋值之后),即使相同的条目在文件的前面被正确地解析过。在
如果我在赋值下面加上
if cvss is None: cvss = "0"
然后,对许多进一步的cvss
的解析会分配它们的正确值(而其他一些值则为None)。在
当使用导致错误解析的<ReportHost>...</reportHost>
并在程序中运行它时,它工作得很好(例如,cvss
被分配了9.3
)。在
我不知道我在代码中犯了一个错误,因为在大量类似的记录中,有些记录被正确地处理了,有些记录没有被正确地处理(有些记录是相同的,但处理方式仍然不同)。我也找不到任何关于失败的记录的特别之处——之前和之后相同的记录都可以。在
从iterparse() docs:
仅在“end”事件完全解析时删除
inReport*
变量并处理ReportHost。使用elementtreeapi从当前ReportHost元素获取必要的信息,如cvss_base_score
。在要保存内存,请执行以下操作:
相关问题 更多 >
编程相关推荐