Q:

How to parse XML and store as a list ( python)

Im sorry have to ask again.
I want to convert xml file to excel by xml.etree.ElementTree.
Assume my xml looks like :

<ParameterCluster>
          <Name>AAAAAA</Name>
          <ParameterDefinitionList>
            <ParameterDefinition>
              <Name>LengthMin</Name>
              <Type>UInt8</Type>
            </ParameterDefinition>
            <ParameterDefinition>
              <Name>LengthMax</Name>
              <Type>UInt8</Type>
            </ParameterDefinition>
          </ParameterDefinitionList>

          <VariantImlementationList>
            <VariantImlementation>
              <MajorVariantList>
                <MajorVariant>A_Basis</MajorVariant>
              </MajorVariantList>
              <MinorVariantList>
                        <ParameterValue>
                          <ValueList>
                            <Value>47</Value>
                          </ValueList>
                          <ValueList>
                            <Value>80</Value>
                          </ValueList>
                        </ParameterValue>
              </MinorVariantList>
              <MajorVariantList>
                <MajorVariant>B_Basis</MajorVariant>
                <MajorVariant>C_Basis</MajorVariant>
              </MajorVariantList>
              <MinorVariantList>
                        <ParameterValue>
                          <ValueList>
                            <Value>47</Value>
                          </ValueList>
                          <ValueList>
                            <Value>40</Value>
                          </ValueList>
                        </ParameterValue>
              </MinorVariantList> 
            </VariantImlementation>
          </VariantImlementationList>
        </ParameterCluster>

That means, I have 3 basis (A_basis, B_basis, C_basis).
And in A_ Basis, the Value of LengthMin is 47 and Value of LengthMax is 80.
But in B_basis and C_Basis. the Value of LengthMin is 47 and Value of LengthMax is 40.
So I want to get something like :
{'AAAAAA','LengthMin','UInt8','A_Basis',47}
{'AAAAAA','LengthMax','UInt8','A_Basis',80}
{'AAAAAA','LengthMin','UInt8','B_Basis',47}
{'AAAAAA','LengthMax','UInt8','B_Basis',40}
{'AAAAAA','LengthMin','UInt8','C_Basis',47}
{'AAAAAA','LengthMax','UInt8','C_Basis',40}

Then I can write it into excel file. Is that possible to get that kind of list?

A:

For parsing XML you can use BeautifulSoup instead of xml.etree.ElementTree (the interface is more intuitive).
The parsing is straightforward (assuming length of ParameterValue is always the same as ParameterValue.ValueList: Firstly you need to extract the the parameter types, and then iterate over all <MajorVariant> and populate result list.
If BeautifulSoup isn't a problem, here is example code:
data = """<ParameterCluster>
              <Name>AAAAAA</Name>
              <ParameterDefinitionList>
                <ParameterDefinition>
                  <Name>LengthMin</Name>
                  <Type>UInt8</Type>
                </ParameterDefinition>
                <ParameterDefinition>
                  <Name>LengthMax</Name>
                  <Type>UInt8</Type>
                </ParameterDefinition>
              </ParameterDefinitionList>

              <VariantImlementationList>
                <VariantImlementation>
                  <MajorVariantList>
                    <MajorVariant>A_Basis</MajorVariant>
                  </MajorVariantList>
                  <MinorVariantList>
                            <ParameterValue>
                              <ValueList>
                                <Value>47</Value>
                              </ValueList>
                              <ValueList>
                                <Value>80</Value>
                              </ValueList>
                            </ParameterValue>
                  </MinorVariantList>
                  <MajorVariantList>
                    <MajorVariant>B_Basis</MajorVariant>
                    <MajorVariant>C_Basis</MajorVariant>
                  </MajorVariantList>
                  <MinorVariantList>
                            <ParameterValue>
                              <ValueList>
                                <Value>47</Value>
                              </ValueList>
                              <ValueList>
                                <Value>40</Value>
                              </ValueList>
                            </ParameterValue>
                  </MinorVariantList>
                </VariantImlementation>
              </VariantImlementationList>
            </ParameterCluster>"""

from bs4 import BeautifulSoup
from pprint import pprint

soup = BeautifulSoup(data, 'xml')

name, types = soup.select_one('Name'), []
for n, t in zip(soup.select('ParameterDefinitionList Name'), soup.select('ParameterDefinitionList Type')):
    types.append([name.text, n.text, t.text])

rv = []
for major, minor in zip(soup.select('MajorVariantList'), soup.select('MajorVariantList ~ MinorVariantList')):
    for mj in major.select('MajorVariant'):
        for i, mn in enumerate(minor.select('Value')):
            rv.append(types[i] + [mj.text, mn.text])

pprint(rv, width=120)

Output:
[['AAAAAA', 'LengthMin', 'UInt8', 'A_Basis', '47'],
 ['AAAAAA', 'LengthMax', 'UInt8', 'A_Basis', '80'],
 ['AAAAAA', 'LengthMin', 'UInt8', 'B_Basis', '47'],
 ['AAAAAA', 'LengthMax', 'UInt8', 'B_Basis', '40'],
 ['AAAAAA', 'LengthMin', 'UInt8', 'C_Basis', '47'],
 ['AAAAAA', 'LengthMax', 'UInt8', 'C_Basis', '40']]

