使用 Python xml.etree.ElementTree 从 xml 文件中按名称获取元素值

发布于 2025-01-18 16:36:26 字数 1381 浏览 1 评论 0原文

我正在使用 python-evtx 模块来解析 Windows 事件日志。我使用 xml.etree.ElementTree 将输出转换为 XML,然后尝试解析每个条目以按名称从某个键值获取值。

我有以下代码来显示不同的键值和我想要访问的文本;

import xml.etree.ElementTree as ET

tree = ET.parse('xmlfile1.txt')
root = tree.getroot()

for x in root[1]:
     print(x.tag, x.attrib, x.text)

输出看起来像这样。

{http://schemas.microsoft.com/win/2004/08/events/event}Data {'Name': 'IpAddress'} -
{http://schemas.microsoft.com/win/2004/08/events/event}Data {'Name': 'IpPort'} -
{http://schemas.microsoft.com/win/2004/08/events/event}Data {'Name': 'ImpersonationLevel'} -
{http://schemas.microsoft.com/win/2004/08/events/event}Data {'Name': 'RestrictedAdminMode'} -
{http://schemas.microsoft.com/win/2004/08/events/event}Data {'Name': 'TargetOutboundUserName'} -
{http://schemas.microsoft.com/win/2004/08/events/event}Data {'Name': 'TargetOutboundDomainName'} -
{http://schemas.microsoft.com/win/2004/08/events/event}Data {'Name': 'VirtualAccount'} %%1843
{http://schemas.microsoft.com/win/2004/08/events/event}Data {'Name': 'TargetLinkedLogonId'} 0x0000000000000000
{http://schemas.microsoft.com/win/2004/08/events/event}Data {'Name': 'ElevatedToken'} %%1842

我想做的是能够获取特定键值的值,例如“{http://schemas.microsoft.com/win/2004/08/events/event}Data {'Name': 'IpAddress '} -" ,但无法弄清楚如何通过键名获取值。

如何从 xml.etree.ElementTree 获取 xml 输出并从特定键/元素获取文本值?

I am using the python-evtx module to parse Windows event logs. I am converting the output to XML using xml.etree.ElementTree and then trying to parse through each entry to get the value from a certain key value by its name.

I have the following code to show the different key values with the text I want to access;

import xml.etree.ElementTree as ET

tree = ET.parse('xmlfile1.txt')
root = tree.getroot()

for x in root[1]:
     print(x.tag, x.attrib, x.text)

The output looks like this.

{http://schemas.microsoft.com/win/2004/08/events/event}Data {'Name': 'IpAddress'} -
{http://schemas.microsoft.com/win/2004/08/events/event}Data {'Name': 'IpPort'} -
{http://schemas.microsoft.com/win/2004/08/events/event}Data {'Name': 'ImpersonationLevel'} -
{http://schemas.microsoft.com/win/2004/08/events/event}Data {'Name': 'RestrictedAdminMode'} -
{http://schemas.microsoft.com/win/2004/08/events/event}Data {'Name': 'TargetOutboundUserName'} -
{http://schemas.microsoft.com/win/2004/08/events/event}Data {'Name': 'TargetOutboundDomainName'} -
{http://schemas.microsoft.com/win/2004/08/events/event}Data {'Name': 'VirtualAccount'} %%1843
{http://schemas.microsoft.com/win/2004/08/events/event}Data {'Name': 'TargetLinkedLogonId'} 0x0000000000000000
{http://schemas.microsoft.com/win/2004/08/events/event}Data {'Name': 'ElevatedToken'} %%1842

What I am trying to do is be able to get the value of a particular key value such as "{http://schemas.microsoft.com/win/2004/08/events/event}Data {'Name': 'IpAddress'} -" , but cannot figure out how to get a value by the key name.

How can get take the xml output from xml.etree.ElementTree and get the text value from a particular key/element?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

孤星 2025-01-25 16:36:26

我想要做的是能够获取特定键值的值,例如 "{http://schemas.microsoft.com/win/2004/08/events/event}Data { '名称': 'IP地址'} -"

使用 XPath 和命名空间映射。

import xml.etree.ElementTree as ET

ns_map = {
  'e': 'http://schemas.microsoft.com/win/2004/08/events/event'
}

tree = ET.parse('xmlfile1.txt')

# specific node
ip_address = tree.find('.//e:EventData/e:Data[@Name="IpAddress"]', ns_map)
if ip_address:
    print(ip_address.text)

# multiple nodes
for data in tree.iterfind('.//e:EventData/e:Data', ns_map):
   print(data.attrib['Name'], data.text)

http://schemas.microsoft.com/win/2004/08/events/event 命名空间中的所有元素都需要 XPath 中相应的命名空间前缀(我选择了 e:,但只要它解析为正确的名称空间 URI,这是任意的),否则将找不到它们。

What I am trying to do is be able to get the value of a particular key value such as "{http://schemas.microsoft.com/win/2004/08/events/event}Data {'Name': 'IpAddress'} -"

Use XPath and a namespace map.

import xml.etree.ElementTree as ET

ns_map = {
  'e': 'http://schemas.microsoft.com/win/2004/08/events/event'
}

tree = ET.parse('xmlfile1.txt')

# specific node
ip_address = tree.find('.//e:EventData/e:Data[@Name="IpAddress"]', ns_map)
if ip_address:
    print(ip_address.text)

# multiple nodes
for data in tree.iterfind('.//e:EventData/e:Data', ns_map):
   print(data.attrib['Name'], data.text)

All the elements that are in the http://schemas.microsoft.com/win/2004/08/events/event namespace need the respective namespace prefix in the XPath (I chose e:, but that's arbitrary as long as it resolves to the right namespace URI), otherwise they will not be found.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文