如何使用 Python 在 Microsoft 自动生成的 XML 文件中查找元素?

问题描述 投票:0回答:1

我正在尝试根据 文档解析表示 Powershell 序列化对象的 xml 文件:

import xml.etree.ElementTree as ET
state = ET.parse('d:\tmp\myxml.file')
stateroot = state.getroot()

运行此命令后,我得到以下信息:

>>> print(stateroot.findtext('failcount'))
None

或者这个:

>>> ET.tostring(stateroot)
b'<ns0:Objs xmlns:ns0="http://schemas.microsoft.com/powershell/2004/04" Version="1.1.0.1">\n  <ns0:Obj RefId="0">\n    <ns0:TN RefId="0">\n      <ns0:T>Deserialized.System.Management.Automation.PSCustomObject</ns0:T>\n      <ns0:T>Deserialized.System.Object</ns0:T>\n    </ns0:TN>\n    <ns0:MS>\n      <ns0:Obj N="lastSuccessDT" RefId="1">\n        <ns0:DT>2024-12-03T16:32:19.0841579+00:00</ns0:DT>\n        <ns0:MS>\n          <ns0:Obj N="DisplayHint" RefId="2">\n            <ns0:TN RefId="1">\n              <ns0:T>Microsoft.PowerShell.Commands.DisplayHintType</ns0:T>\n              <ns0:T>System.Enum</ns0:T>\n              <ns0:T>System.ValueType</ns0:T>\n              <ns0:T>System.Object</ns0:T>\n            </ns0:TN>\n            <ns0:ToString>DateTime</ns0:ToString>\n            <ns0:I32>2</ns0:I32>\n          </ns0:Obj>\n        </ns0:MS>\n      </ns0:Obj>\n      <ns0:Obj N="lastFailureDT" RefId="3">\n        <ns0:DT>2024-12-02T20:57:48.8872097+00:00</ns0:DT>\n        <ns0:MS>\n          <ns0:Obj N="DisplayHint" RefId="4">\n            <ns0:TN RefId="2">\n              <ns0:T>Deserialized.Microsoft.PowerShell.Commands.DisplayHintType</ns0:T>\n              <ns0:T>Deserialized.System.Enum</ns0:T>\n              <ns0:T>Deserialized.System.ValueType</ns0:T>\n              <ns0:T>Deserialized.System.Object</ns0:T>\n            </ns0:TN>\n            <ns0:ToString>DateTime</ns0:ToString>\n            <ns0:I32>2</ns0:I32>\n          </ns0:Obj>\n        </ns0:MS>\n      </ns0:Obj>\n      <ns0:I32 N="failcount">0</ns0:I32>\n    </ns0:MS>\n  </ns0:Obj>\n</ns0:Objs>'

我真正想要的是能够从这些文件中检索lastSuccessDT、lastFailureDT 和failcount 值。

d:mp\myxml.文件内容示例:

<Objs Version="1.1.0.1" xmlns="http://schemas.microsoft.com/powershell/2004/04">
  <Obj RefId="0">
    <TN RefId="0">
      <T>Deserialized.System.Management.Automation.PSCustomObject</T>
      <T>Deserialized.System.Object</T>
    </TN>
    <MS>
      <Obj N="lastSuccessDT" RefId="1">
        <DT>2024-12-03T16:32:19.0841579+00:00</DT>
        <MS>
          <Obj N="DisplayHint" RefId="2">
            <TN RefId="1">
              <T>Microsoft.PowerShell.Commands.DisplayHintType</T>
              <T>System.Enum</T>
              <T>System.ValueType</T>
              <T>System.Object</T>
            </TN>
            <ToString>DateTime</ToString>
            <I32>2</I32>
          </Obj>
        </MS>
      </Obj>
      <Obj N="lastFailureDT" RefId="3">
        <DT>2024-12-02T20:57:48.8872097+00:00</DT>
        <MS>
          <Obj N="DisplayHint" RefId="4">
            <TN RefId="2">
              <T>Deserialized.Microsoft.PowerShell.Commands.DisplayHintType</T>
              <T>Deserialized.System.Enum</T>
              <T>Deserialized.System.ValueType</T>
              <T>Deserialized.System.Object</T>
            </TN>
            <ToString>DateTime</ToString>
            <I32>2</I32>
          </Obj>
        </MS>
      </Obj>
      <I32 N="failcount">0</I32>
    </MS>
  </Obj>
</Objs>
python xml powershell
1个回答
0
投票

搜索具有特定属性的标签:

import xml.etree.ElementTree as ET

xml_str = """<Objs Version="1.1.0.1" xmlns="http://schemas.microsoft.com/powershell/2004/04">
  <Obj RefId="0">
    <TN RefId="0">
      <T>Deserialized.System.Management.Automation.PSCustomObject</T>
      <T>Deserialized.System.Object</T>
    </TN>
    <MS>
      <Obj N="lastSuccessDT" RefId="1">
        <DT>2024-12-03T16:32:19.0841579+00:00</DT>
        <MS>
          <Obj N="DisplayHint" RefId="2">
            <TN RefId="1">
              <T>Microsoft.PowerShell.Commands.DisplayHintType</T>
              <T>System.Enum</T>
              <T>System.ValueType</T>
              <T>System.Object</T>
            </TN>
            <ToString>DateTime</ToString>
            <I32>2</I32>
          </Obj>
        </MS>
      </Obj>
      <Obj N="lastFailureDT" RefId="3">
        <DT>2024-12-02T20:57:48.8872097+00:00</DT>
        <MS>
          <Obj N="DisplayHint" RefId="4">
            <TN RefId="2">
              <T>Deserialized.Microsoft.PowerShell.Commands.DisplayHintType</T>
              <T>Deserialized.System.Enum</T>
              <T>Deserialized.System.ValueType</T>
              <T>Deserialized.System.Object</T>
            </TN>
            <ToString>DateTime</ToString>
            <I32>2</I32>
          </Obj>
        </MS>
      </Obj>
      <I32 N="failcount">0</I32>
    </MS>
  </Obj>
</Objs>"""

root = ET.fromstring(xml_str)
ns = {'': 'http://schemas.microsoft.com/powershell/2004/04'}

ls = root.find('.//Obj[@N="lastSuccessDT"]/DT', ns).text
print(f"lastSuccessDT: {ls}")

lf = root.find('.//Obj[@N="lastFailureDT"]/DT', ns).text
print(f"lastFailureDT: {lf}")

fc = root.find('.//I32[@N="failcount"]', ns).text
print(f"failcount: {fc}")

输出:

lastSuccessDT: 2024-12-03T16:32:19.0841579+00:00
lastFailureDT: 2024-12-02T20:57:48.8872097+00:00
failcount: 0
© www.soinside.com 2019 - 2024. All rights reserved.