Asmature
Asmature

Reputation: 15

Parsing an XML file to retrieve parents according to child element

I would like to parse an xml file, here's my xml file:

<group>
  <name>Services</name>
  <file>
    <name>Path\File1.c</name>
  </file>
  <file>
    <name>Path\File2.c</name>
    <excluded>
      <configuration>Configuration1</configuration>
      <configuration>Configuration2</configuration>
    </excluded>
  </file>
  <file>
    <name>Path\File3.c</name>
    <excluded>
      <configuration>Configuration2</configuration>
      <configuration>Configuration3</configuration>
    </excluded>
  </file>
  <file>
    <name>Path\File4.c</name>
  </file>
</group>

This xml file describes the file used within a project. This project has multiple Configuration that are named Configuration1 to Configuration4. For the example let's assume I have access to those thanks to a list of configuration name. The xml file list every file used in the project, each file is in every configuration unless below the file name, the configuration is within the excluded tags

What I would like to achieve is a function that:

I've successfully retrieve every files, here's my code:

from lxml import etree

def getSourceFile(sTree, szConfigName):
  #retrieve every file used in the project
  lSource = []
  for data in sTree.xpath('/group'):
      file = data.findall("file")
      for x in file:
        for element in x:
          if(element.tag == "name"):          
              lSource.append(element.text)
  print(lSource)

if __name__ == '__main__':
  sTree = etree.parse("myXmlFile.xml")
  lConfigName = ["Configuration1", "Configuration2", "Configuration3", "Configuration4"]

  for iIdxConfig in range(0, len(lConfigName)):
    getSourceFile(sTree, lConfigName[iIdxConfig])
    print("\n\n")

I don't know how I can proceed to check for each file if the current configuration is excluding this file.

Upvotes: 1

Views: 101

Answers (1)

Alex
Alex

Reputation: 26

I'm not sure to well understand what you really want to do but maybe this snippet can help you

from lxml import etree

def getSourceFile(sTree, szConfigName):
#retrieve every file used in the project
lSource = []
for data in sTree.xpath('/group'):
  file = data.findall("file")
  for x in file:
    myName = ""
    confIsExcluded=False
    for element in x:
      if(element.tag == "name"):
        myName = element.text
      if(element.tag == "excluded"):
        configurations = [config.text for config in element.findall("configuration")]
        if(szConfigName in configurations):
          confIsExcluded=True
    if(not confIsExcluded):
      lSource.append(myName)
print(lSource)

if __name__ == '__main__':
  sTree = etree.parse("myXmlFile.xml")
  lConfigName = ["Configuration1", "Configuration1", "Configuration2", "Configuration3"]

  for iIdxConfig in range(0, len(lConfigName)):
  getSourceFile(sTree, lConfigName[iIdxConfig])
  print("\n\n")

Upvotes: 1

Related Questions