【问题标题】:Parsing XML using xml.etree ( only )使用 xml.etree 解析 XML(仅限)
【发布时间】:2014-05-02 15:25:41
【问题描述】:

使用 xml.etree(请使用此模块)

我该如何解析:

<?xml version="1.0" encoding="UTF-8"?>
<EntityPath="c:\a.zip" Name="a.zip" >
   <WorkfileDescription>something</WorkfileDescription>
   <Revision EntityPath="c:\a.zip" Name="1.1" Author="me">
      <ChangeDescription>Some comentary</ChangeDescription>
      <PGROUP Name="A" />
      <PGROUP Name="B" />
      <PGROUP Name="C" />
      <Label Name="SOFTWARE" />
      <Label Name="READY" />
   </Revision>
   <Revision EntityPath="c:\a.zip" Name="1.0" Author="me">
      <ChangeDescription>Some comentary</ChangeDescription>
      <PGROUP Name="A" />
      <Label Name="GAME" />
      <Label Name="READY" />
   </Revision>
</VersionedFile>

为了得到:

Revision: a.zip
Name: 1.1
Author: me
ChangeDescription: Some comentary
PGROUP: A
PGROUP: B
PGROUP: C
Label: SOFTWARE
Label: READY

Revision: a.zip
Name: 1.0
Author: me
ChangeDescription: Some comentary
PGROUP: A
Label: GAME
Label: READY

到目前为止,使用以下代码我只能获得 Revision 行,但我正在努力解析其他子字段:

from xml.etree import ElementTree
try:
    tree = ElementTree.parse(self.xml)
    root = tree.getroot()
    info_list = []
    for child in root:
        print(child.tag,child.attrib)


except Exception:
    raise
finally:
    self.xml = None

【问题讨论】:

    标签: python xml python-2.7 xml.etree


    【解决方案1】:

    找到所有Revision标签,打印来自element.attrib的所有属性,遍历Revision元素以获取子元素和Name属性值:

    import xml.etree.ElementTree as etree
    
    
    data = """<?xml version="1.0" encoding="UTF-8"?>
    <VersionedFile EntityPath="c:\\a.zip" Name="VfOMP_CRM.zip">
       <WorkfileDescription>something</WorkfileDescription>
       <Revision EntityPath="c:\\a.zip" Name="1.1" Author="me">
          <ChangeDescription>Some comentary</ChangeDescription>
          <PGROUP Name="A" />
          <PGROUP Name="B" />
          <PGROUP Name="C" />
          <Label Name="SOFTWARE" />
          <Label Name="READY" />
       </Revision>
       <Revision EntityPath="c:\\a.zip" Name="1.0" Author="me">
          <ChangeDescription>Some comentary</ChangeDescription>
          <PGROUP Name="A" />
          <Label Name="GAME" />
          <Label Name="READY" />
       </Revision>
    </VersionedFile>
    """
    
    tree = etree.fromstring(data)
    for revision in tree.findall('Revision'):
        for key, value in revision.attrib.iteritems():
            print "%s: %s" % (key, value)
    
        for child in revision:
            print "%s: %s" % (child.tag, child.attrib.get('Name', ''))
    
        print
    

    打印:

    Name: 1.1
    EntityPath: c:\a.zip
    Author: me
    ChangeDescription: 
    PGROUP: A
    PGROUP: B
    PGROUP: C
    Label: SOFTWARE
    Label: READY
    
    Name: 1.0
    EntityPath: c:\a.zip
    Author: me
    ChangeDescription: 
    PGROUP: A
    Label: GAME
    Label: READY
    

    您可能需要稍微调整一下以获得所需的输出,但这应该会给您基本的想法。

    【讨论】:

      【解决方案2】:

      基于 alecxe 解决方案,我能够使用它:

          from xml.etree import ElementTree
          try:
              tree = ElementTree.parse(self.xml)
              info_list = []
              for revision in tree.findall('Revision'):
                  for key, value in revision.attrib.iteritems():
                      values = dict()
                      values[key] = value
                      info_list.append(values)
                      #print "%s: %s" % (key, value)
                  for child in revision:
                      values = dict()
                      # this is needed to match the change description field.
                      if child.tag == 'ChangeDescription':
                          values[child.tag] = child.text
                          #print "%s: %s" % (child.tag, child.text)
                      else:
                          values[child.tag] = child.attrib.get('Name', '')
                          #print "%s: %s" % (child.tag, child.attrib.get('Name', ''))
                      info_list.append(values)
      
                  print
      
              for i in info_list:
                  print(i)
      
          except Exception:
              raise
          finally:
              self.xml = None
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-05-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-12-26
        • 2020-11-02
        • 1970-01-01
        相关资源
        最近更新 更多