0

I am working on a Python script in which I need to create an array of the following type. How can I make this type of array dynamically?

types = [
    Type('First'),
    Type('Second'),
    Type('Third', ['ThirdChild1', 'ThirdChild2']),
    Type('Fourth', ['FourthChild1', 'FourthChild2', 'FourthChild3'])
]

Here is the test.xml file

<?xml version="1.0" encoding="UTF-8"?>
<Package xmlns="http://soap.sforce.com/2006/04/metadata">

    <types>
        <members>*</members>
        <name>First</name>
    </types>

    <types>
        <members>*</members>
        <name>Second</name>
    </types>

    <types>
        <members>ThirdChild1</members>
        <members>ThirdChild2</members>
        <name>Third</name>
    </types>

    <types>
        <members>FourthChild1</members>
        <members>FourthChild2</members>
        <members>FourthChild3</members>
        <name>Fourth</name>
    </types>

    <version>38.0</version>
</Package>

and this is how get the names and elements from xml file

import xml.etree.ElementTree as ET

tree = ET.parse('test.xml')
root = tree.getroot()

name = ET.Element("name")
members = ET.Element("members")

def retrieveType():
    for elem in root:
        for subelem in elem:
            if name.tag == str(subelem.tag).split('}')[1]:
                print('----------', subelem.text)

This subelem.text has the members and name, I want to put them in the array.

4
  • What's ET? Can you include the import? Commented Sep 18, 2019 at 14:20
  • thanks, i update the question. Commented Sep 18, 2019 at 14:21
  • 1
    Your XML Document is not in correct format can you please fix that? Commented Sep 18, 2019 at 16:30
  • @Jab thanks, actually closing tag was not indented properly Commented Sep 19, 2019 at 3:39

1 Answer 1

1

An example using built-in types based on your approach is shown as follows:

import xml.etree.ElementTree as ET

tree = ET.parse('test.xml')
root = tree.getroot()

name = ET.Element("name")
members = ET.Element("members")

def retrieveType():
    result = {}  # create an empty dictionary
    for elem in root:
        cur_members = []  # create an empty list
        cur_name = ""
        for subelem in elem:
            if name.tag == str(subelem.tag).split('}')[1]:
                cur_name = subelem.text
            if members.tag == str(subelem.tag).split('}')[1]:
                cur_members.append(subelem.text)  # add members to the list
        result[cur_name] = cur_members  # add the name/members to the dictionary
    return result


result = retrieveType()

You could adapt something like that to using an array of custom Type objects. See data structures for more information.

Edit: Here is the final solution provided by Anurag Shrivastava:

import xml.etree.ElementTree as ET

tree = ET.parse('test.xml')
root = tree.getroot()

name = ET.Element("name")
members = ET.Element("members")

def retrieveType():
    finalResult = []
    for elem in root:
        cur_members = []
        cur_name = ""
        for subelem in elem:
            if name.tag == str(subelem.tag).split('}')[1]:
                cur_name = subelem.text
            if members.tag == str(subelem.tag).split('}')[1]:
                cur_members.append(subelem.text)
        finalResult.append('Type(' + str(cur_name) + ', ' + str(cur_members) + ')')
    return finalResult
Sign up to request clarification or add additional context in comments.

2 Comments

thanks for the answer, it saved my day. I add a line in your answer which gives me the exact solution.
You are welcome, I edited the answer to include your final solution, thanks for providing it!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.