0

Hello im writing a bit of code im Maya and running into some issues with ElementTree. I need help reading in this xml, or something similar. The XML is generated based on a selection, so it can change.

<root>
    <Locations>
        <1 name="CacheLocation">C:\Users\daunish\Desktop</1>
    </Locations>
    <Objects>
        <1 name="Sphere">[u'pSphere1', u'pSphere2']</1>
        <2 name="Cube">[u'pCube1']</2>
    </Objects>
</root>

I need a way of searching for a particular "name" inside "Locations", and passing the text to a variable.

I also need a way of going through each line inside of "Objects" and preforming a functions, as in a for loop.

I'm open to all suggestions, I have been going crazy trying to get this to work. If you think i should format the XML differently I'm up for that as well. Thanks in advance for the help.

1 Answer 1

1

[Note: your XML is not well formed because you can't have tags that start with a number]
Not sure what you've tried but there are many ways to do this, here's one:

Find the first element with name=CacheLocation in Locations:

>>> filename = root.find("./Locations/*[@name='CacheLocation']").text
>>> filename
'C:\\Users\\daunish\\Desktop'

Iterating over all the elements in Objects:

>>> import ast
>>> for target in root.find("./Objects"):
...     for i in ast.literal_eval(target.text):
...         print(target.get('name'), i)
Sphere pSphere1
Sphere pSphere2
Cube pCube1
Sign up to request clarification or add additional context in comments.

1 Comment

Awesome man! I had no idea i couldn't have tags that started with numbers. Finally things are starting to look up, really appreciate 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.