如何读取下面的XML并获取“host”,“status”,“owner”,“user-template-01”和“test-id”的值?

2024-05-23 21:46:10 发布

您现在位置:Python中文网/ 问答频道 /正文

XML = """<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Entities TotalResults="101" PageSize="100" PageNumber="1">
  <Entity Type="run">
    <Fields>
      <Field Name="host">
        <Value>osdc-vw64</Value>
      </Field>
      <Field Name="status">
        <Value>Passed</Value>
      </Field>
      <Field Name="owner">
        <Value>Aspeg</Value>
      </Field>
      <Field Name="user-template-01">
        <Value>1941896</Value>
      </Field>
      <Field Name="test-id">
        <Value>72769</Value>
      </Field>
    </Fields>
  </Entity>
  <Entity Type="run">
    <Fields>
      <Field Name="host">
        <Value>osdc-57</Value>
      </Field>
      <Field Name="status">
        <Value>Passed</Value>
      </Field>
      <Field Name="owner">
        <Value>spana</Value>
      </Field>
      <Field Name="user-template-01">
        <Value>1941896</Value>
      </Field>
      <Field Name="test-id">
        <Value>72769</Value>
      </Field>
    </Fields>
  </Entity>
  </Entities>"""

我用过:

from xml.etree import ElementTree as ET
root = ET.fromstring(XML)
print root.tag

我不知道现在该怎么办。。。你知道吗


Tags: runnamehostfieldfieldsvaluetypestatus
3条回答

最简单的方法是使用PyQuery(如果您了解jQuery选择器):

from pyquery import PyQuery

query = PyQuery(xml);
host = query("[Name='host'] value").text()
test_id = query("[Name='test-id'] value").text()

由于您有多个Name='host'的元素,因此应该迭代实体:

from pyquery import PyQuery

def process_Entity(entity):
    pass #do something

query = PyQuery(xml);

query("Entity").each(process_Entity)

使用lxml.etree

import lxml.etree as ET

XML = """ your string here """
tree = ET.fromstring(XML) # you may get errors here because of encoding
                          # if so, re.sub(r"\bencoding="[^"]+?", '', XML) works

info_you_need = {entity: {field.get("Name"): field.find("Value").text for field in entity.findall("Fields/Field")} for entity in tree.findall("Entity")}

注意:我对lxml模块非常糟糕,有人可能会想出比这个更好的解决方案:)我的输出是:

{<Element Entity at 0x2af4e18>: {'user-template-01': '1941896', 'owner': 'spana', 'test-id': '72769', 'status': 'Passed', 'host': 'osdc-57'},
 <Element Entity at 0x2af4e40>: {'user-template-01': '1941896', 'owner': 'Aspeg', 'test-id': '72769', 'status': 'Passed', 'host': 'osdc-vw64'}}
import xml.etree.ElementTree as ET
tree = ET.parse('hai.xml')
root = tree.getroot()
for child in root:
  print child.tag, child.attrib
  for a in child:
    print a.tag
    for b in a:
       print b.attrib , b[0].text

相关问题 更多 >