Python XML子树解析

Python XML子树解析,python,xml,Python,Xml,我必须使用lxml甚至XML.etree.ElementTree模块解析XML文件 <?xml version="1.0"?> <corners> <version>1.05</version> <process> <name>ss649</name> <statistics> <statistic name="Min" forparameter="modna

我必须使用lxml甚至XML.etree.ElementTree模块解析XML文件

<?xml version="1.0"?>
<corners>
  <version>1.05</version>
  <process>
    <name>ss649</name>
    <statistics>
      <statistic name="Min" forparameter="modname" isextreme="no" style="tbld">
        <value>0.00073</value>
        <real_value>7.300e-10</real_value>
      </statistic>
      <statistic name="Max" forparameter="modname" isextreme="no" style="tbld">
        <value>0.32420</value>
        <real_value>3.242e-07</real_value>
     </statistic>
     <variant>
          <name>Unit</name>
          <value>
            <value>Size</value>
            <statistics>
              <statistic name="Min" forparameter="modname1" isextreme="no" style="tbld">
                <value>0.02090</value>
                <real_value>2.090e-08</real_value>
              </statistic>
              <statistic name="Max" forparameter="modname2" isextreme="no" style="tbld">
                <value>0.02090</value>
                <real_value>2.090e-08</real_value>
              </statistic>
         </variant>

您可能希望查看这个相当不错的ActiveState代码段:

我是通过以下SO帖子发现这一点的,这也可能有用:

此外,xmltodict也是一个不错的选择:

绝对是你应该考虑使用的东西:

from pprint import pprint
import xmltodict

data = """<?xml version="1.0"?>
<corners>
  <version>1.05</version>
  <process>
    <name>ss649</name>
    <statistics>
      <statistic name="Min" forparameter="modname" isextreme="no" style="tbld">
        <value>0.00073</value>
        <real_value>7.300e-10</real_value>
      </statistic>
      <statistic name="Max" forparameter="modname" isextreme="no" style="tbld">
        <value>0.32420</value>
        <real_value>3.242e-07</real_value>
     </statistic>
    </statistics>
  </process>
</corners>"""

pprint(xmltodict.parse(data))
从pprint导入pprint
导入xmltodict
data=”“”
1.05
ss649
0.00073
7.300e-10
0.32420
3.242e-07
"""
pprint(xmltodict.parse(数据))
一行代码,你就可以开始了


希望对您有用。

我使用了
xml.etree.ElementTree
模块

dict = {}
tree = ET.parse('file.xml')
root=tree.getroot()
for attribute in root:
        for stats in attribute.iter('statistics'):  #Accessing to child tree of the process 'attribute'
            for sub_att in stats.iter('statistic'): #Iterating trough the attribute items
                    name      =  sub_att.get('name')
                    parameter =  sub_att.get('forparameter')
                    for param_value in sub_att.iter('value'):
                         value = param_value.text   #Collecting the value of the sub_attribute
                         break                      #Speed up the script, skips the <real_value>
            if not dict.has_key(parameter):
                    dict[parameter] = {}
            dict[parameter][name] = value
dict = {}
tree = ET.parse('file.xml')
root=tree.getroot()
for attribute in root:
        for stats in attribute.iter('statistics'):  #Accessing to child tree of the process 'attribute'
            for sub_att in stats.iter('statistic'): #Iterating trough the attribute items
                    name      =  sub_att.get('name')
                    parameter =  sub_att.get('forparameter')
                    for param_value in sub_att.iter('value'):
                         value = param_value.text   #Collecting the value of the sub_attribute
                         break                      #Speed up the script, skips the <real_value>
            if not dict.has_key(parameter):
                    dict[parameter] = {}
            dict[parameter][name] = value
dict={
      'modname' : { 
        'Min' : 0.00073,
        'Max': 0.32420,
       }
}