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


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

我不知道该怎么做了

最简单的方法是使用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'的元素,你应该遍历Entities:

from pyquery import PyQuery
def process_Entity(entity):
    pass #do something
query = PyQuery(xml);
query("Entity").each(process_Entity)
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

使用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'}}

相关内容

最新更新