Запрос дочерних значений при итерации по xml - PullRequest
0 голосов
/ 30 октября 2019

Я анализирую xml, в котором есть несколько записей, аналогичных приведенным ниже:

<ReportItem port="22" svc_name="ssh" protocol="tcp" severity="0" pluginID="22964" 
 pluginName="Service Detection" pluginFamily="Service detection">
<description>Nessus was able to identify the remote service by its banner or by looking at the error 
 message it sends when it receives an HTTP request.</description>
<fname>find_service.nasl</fname>
<plugin_modification_date>2019/08/14</plugin_modification_date>
<plugin_name>Service Detection</plugin_name>
<plugin_publication_date>2007/08/19</plugin_publication_date>
<plugin_type>remote</plugin_type>
<risk_factor>None</risk_factor>
<script_version>1.177</script_version>
<solution>n/a</solution>
<synopsis>The remote service could be identified.</synopsis>
<plugin_output>An SSH server is running on this port.</plugin_output>
</ReportItem>

Я хочу запросить текстовое значение plugin_name

hostIter = iter(hostsByIP)
for host in hostIter:
    reportIter = iter(host.elements.childNodes)
    for reportItem in reportIter:
            childIter = iter(reportItem.childNodes)
            for reportChild in childIter:
                print(reportChild.nodeValue)
                #if child.nodeValue == "Traceroute Information":

reportChild.nodeValue возвращает 'Нет '' / n '' Нет '... и т. Д.

reportChild.value выдает ошибку' Текст 'объект не имеет атрибута' value '

reportChild.localName правильно возвращает' plugin_name'и т. д., но также' нет '(что означает, что представляет текстовый узел?)

Ответы [ 2 ]

0 голосов
/ 30 октября 2019

Необходимо проверить тип узла перед попыткой прочитать значение:

hostIter = iter(hostsByIP)
for host in hostIter:
    reportIter = iter(host.elements.childNodes)
    for reportItem in reportIter:
            childIter = iter(reportItem.childNodes)
            for reportChild in childIter:
                if reportChild.nodeType == 1:
                    print(reportChild.firstChild.nodeValue)
                    #if child.nodeValue == "Traceroute Information":
0 голосов
/ 30 октября 2019

Вы можете сделать это следующим образом, используя xpath выражение "./ReportItem/plugin_name"

import xml.etree.ElementTree as ET

data = '''<?xml version="1.0"?><data><ReportItem port="22" svc_name="ssh" protocol="tcp" severity="0" pluginID="22964"   pluginName="Service Detection" pluginFamily="Service detection"> <description>Nessus was able to identify the remote service by its banner or by looking at the error   message it sends when it receives an HTTP request.</description> <fname>find_service.nasl</fname> <plugin_modification_date>2019/08/14</plugin_modification_date> <plugin_name>Service Detection</plugin_name> <plugin_publication_date>2007/08/19</plugin_publication_date> <plugin_type>remote</plugin_type> <risk_factor>None</risk_factor> <script_version>1.177</script_version> <solution>n/a</solution> <synopsis>The remote service could be identified.</synopsis> <plugin_output>An SSH server is running on this port.</plugin_output> </ReportItem><ReportItem port="22" svc_name="ssh" protocol="tcp" severity="0" pluginID="22964"   pluginName="Service Detection" pluginFamily="Service detection"> <description>Nessus was able to identify the remote service by its banner or by looking at the error   message it sends when it receives an HTTP request.</description> <fname>find_service.nasl</fname> <plugin_modification_date>2019/08/14</plugin_modification_date> <plugin_name>Service Detection2</plugin_name> <plugin_publication_date>2007/08/19</plugin_publication_date> <plugin_type>remote</plugin_type> <risk_factor>None</risk_factor> <script_version>1.177</script_version> <solution>n/a</solution> <synopsis>The remote service could be identified.</synopsis> <plugin_output>An SSH server is running on this port.</plugin_output> </ReportItem></data>'''

root = ET.fromstring(data) 
for report in root.findall("./ReportItem/plugin_name"):
   print(report.text)

выход: Обнаружение службы Обнаружение службы2

...