从 XML 获取特定值

Get specific value from XML

我正在尝试使用 XML.etree.elementtree 从 XML 文档中获取特定值。 XML 看起来像这样: XML example

<?xml version="1.0" encoding="UTF-8"?><evt:event xmlns:evt="http://www.portauthoritytech.com/schmea/event/1.0" xmlns:fn="http://www.w3.org/2005/02/xpath-functions"><evt:event_info><evt:incidentId>655344</evt:incidentId><evt:eventId>11403596976460963589</evt:eventId><evt:subject>testem jestem </evt:subject><evt:insert_date>2021-02-26 08:46:07.407</evt:insert_date><evt:serviceId>183833403</evt:serviceId><evt:channel><evt:service_name>SMTP</evt:service_name><evt:agent_name>Test</evt:agent_name><evt:protocol>PROTOCOL_SMTP</evt:protocol></evt:channel><evt:source><evt:event_user><evt:commonName>Barbara Jones</evt:commonName><evt:username>barbara</evt:username><evt:full_name>Barbara Jones</evt:full_name><evt:login_name>barbara</evt:login_name><evt:hostname/><evt:port/><evt:email>barbara@example.com</evt:email><evt:ip/><evt:extra_data/><evt:domain/></evt:event_user></evt:source><evt:destinations><evt:Count>1</evt:Count><evt:destination><evt:event_user><evt:commonName>John Smith</evt:commonName><evt:username/><evt:hostname/><evt:port/><evt:email>John@example.com</evt:email><evt:ip/><evt:extra_data/><evt:domain/></evt:event_user><evt:destination_type>TO</evt:destination_type><evt:action_taken><evt:action_type>2097152</evt:action_type></evt:action_taken><evt:released_by/><evt:release_date/></evt:destination></evt:destinations></evt:event_info><evt:action_taken><evt:action_type>2097152</evt:action_type><evt:name>AUTHORIZED</evt:name></evt:action_taken><evt:extra_data/><evt:destinationList>John@examplecom</evt:destinationList><evt:rules><evt:Count>1</evt:Count><evt:rule><evt:rule_id>655345</evt:rule_id><evt:policy_name>Test Policy</evt:policy_name><evt:rule_name>Pizzatest</evt:rule_name><evt:is_rule>true</evt:is_rule><evt:exception_parent_name/><evt:threshold>9</evt:threshold><evt:severity>LOW</evt:severity><evt:action_taken><evt:action_type/></evt:action_taken><evt:matches>9</evt:matches><evt:event_content_classifiers><evt:Count>1</evt:Count><evt:event_content_classifier><evt:classifier>pizzatest</evt:classifier><evt:classifier_type>KEY_PHRASE</evt:classifier_type><evt:matches>9</evt:matches><evt:event_breached_contents><evt:Count>1</evt:Count><evt:event_breached_content><values><value>Pizzatest</value></values><evt:matches>0</evt:matches><evt:breach_location_type>BODY</evt:breach_location_type><evt:breach_location_name>/var/spool/postfix/tmp//D6EDBEFCE499741098AC.eml|||Transaction Body.txt</evt:breach_location_name></evt:event_breached_content></evt:event_breached_contents></evt:event_content_classifier></evt:event_content_classifiers></evt:rule></evt:rules><evt:detected_by/></evt:event>

我只想从特定字段中获取值。 例如: CommonName 来自 event_user。如何实现?我正在努力查找、查找和迭代,但没有结果。

迄今为止我用这个取得的最好成绩。但我只想获得有关芭芭拉的信息。换句话说 - 我想指出我感兴趣的确切元素。

root=ET.parse(myFile)
 for tag in root.iter('{http://www.portauthoritytech.com/schmea/event/1.0}email'):
     print(tag.text)

barbara@example.com
John@example.com

希望你能帮助我。我专门为它创建了帐户。

更改为使用 ElementTree 而不是 lxml。

from xml.etree import ElementTree
from io import BytesIO

data = b'''<?xml version="1.0" encoding="UTF-8"?>
<evt:event xmlns:evt="http://www.portauthoritytech.com/schmea/event/1.0" xmlns:fn="http://www.w3.org/2005/02/xpath-functions">
    <evt:event_info>
        <evt:incidentId>655344</evt:incidentId>
        <evt:eventId>11403596976460963589</evt:eventId>
        <evt:subject>testem jestem</evt:subject>
        <evt:insert_date>2021-02-26 08:46:07.407</evt:insert_date>
        <evt:serviceId>183833403</evt:serviceId>
        <evt:channel>
            <evt:service_name>SMTP</evt:service_name>
            <evt:agent_name>Test</evt:agent_name>
            <evt:protocol>PROTOCOL_SMTP</evt:protocol>
        </evt:channel>
        <evt:source>
            <evt:event_user>
                <evt:commonName>Barbara Jones</evt:commonName>
                <evt:username>barbara</evt:username>
                <evt:full_name>Barbara Jones</evt:full_name>
                <evt:login_name>barbara</evt:login_name>
                <evt:hostname/>
                <evt:port/>
                <evt:email>barbara@example.com</evt:email>
                <evt:ip/>
                <evt:extra_data/>
                <evt:domain/>
            </evt:event_user>
        </evt:source>
        <evt:destinations>
            <evt:Count>1</evt:Count>
            <evt:destination>
                <evt:event_user>
                    <evt:commonName>John Smith</evt:commonName>
                    <evt:username/>
                    <evt:hostname/>
                    <evt:port/>
                    <evt:email>John@example.com</evt:email>
                    <evt:ip/>
                    <evt:extra_data/>
                    <evt:domain/>
                </evt:event_user>
                <evt:destination_type>TO</evt:destination_type>
                <evt:action_taken>
                    <evt:action_type>2097152</evt:action_type>
                </evt:action_taken>
                <evt:released_by/>
                <evt:release_date/>
            </evt:destination>
        </evt:destinations>
    </evt:event_info>
</evt:event>
'''

ns = {"evt": "http://www.portauthoritytech.com/schmea/event/1.0"}
f = BytesIO(data)
tree = ElementTree.parse(f)

for event_user in tree.findall(".//evt:event_user", ns):
    print(event_user.find('evt:commonName', ns).text)