0

I have had a question about Elemttree parsing before and it was answered the next day. I am still not a pro in python and therefore need help once more.

<MiddleCommand xsi:type="SetEndPoseToleranceType" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
        <CommandID>19827</CommandID>
        <Tolerance>
            <XPointTolerance>3.0</XPointTolerance>
            <YPointTolerance>3.0</YPointTolerance>
            <ZPointTolerance>2.0</ZPointTolerance>
            <XAxisTolerance>2.0</XAxisTolerance>
            <ZAxisTolerance>2.0</ZAxisTolerance>
        </Tolerance>
    </MiddleCommand>
    <MiddleCommand xsi:type="MoveToType" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
        <CommandID>19828</CommandID>
        <MoveStraight>false</MoveStraight>
        <EndPosition>
            <Point>
                <X>528.65</X>
                <Y>33.8</Y>
                <Z>50.0</Z>
            </Point>
            <XAxis>
                <I>-0.7071067811865475</I>
                <J>-0.7071067811865477</J>
                <K>-0.0</K>
            </XAxis>
            <ZAxis>
                <I>0.0</I>
                <J>0.0</J>
                <K>-1.0</K>
            </ZAxis>
        </EndPosition>
    </MiddleCommand>

This is part of my XML file. I have been able to get my Values from different Command types and write them into classes. The only thing that i cant manage to do is. I want an array with Commands. So that i know the order which one is the first to happen So ultimately the array should look something like this:

Array_Commands = []
Array_Commands[0] = SetEndPoseToleranceType
Array_Commands[1] = MoveToType

Obviously I want to loop through the XML and not create the Array by hand. I dont have any code regarding this problem so far.

I hope someone knows a simple solution. Best regard and thank you all!

1
  • 1
    Please post the python code to parse and populate objects. Commented Aug 21, 2021 at 14:18

1 Answer 1

1

See below

import xml.etree.ElementTree as ET


xml = '''<r><MiddleCommand xsi:type="SetEndPoseToleranceType" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
        <CommandID>19827</CommandID>
        <Tolerance>
            <XPointTolerance>3.0</XPointTolerance>
            <YPointTolerance>3.0</YPointTolerance>
            <ZPointTolerance>2.0</ZPointTolerance>
            <XAxisTolerance>2.0</XAxisTolerance>
            <ZAxisTolerance>2.0</ZAxisTolerance>
        </Tolerance>
    </MiddleCommand>
    <MiddleCommand xsi:type="MoveToType" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
        <CommandID>19828</CommandID>
        <MoveStraight>false</MoveStraight>
        <EndPosition>
            <Point>
                <X>528.65</X>
                <Y>33.8</Y>
                <Z>50.0</Z>
            </Point>
            <XAxis>
                <I>-0.7071067811865475</I>
                <J>-0.7071067811865477</J>
                <K>-0.0</K>
            </XAxis>
            <ZAxis>
                <I>0.0</I>
                <J>0.0</J>
                <K>-1.0</K>
            </ZAxis>
        </EndPosition>
    </MiddleCommand></r>'''

root = ET.fromstring(xml)
cmds = [cmd.attrib['{http://www.w3.org/2001/XMLSchema-instance}type'] for cmd in root.findall('.//MiddleCommand')]
print(cmds)

output

['SetEndPoseToleranceType', 'MoveToType']
Sign up to request clarification or add additional context in comments.

Comments

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.