1
<?xml version="1.0" ?> 
<Data>  
    <Channels>
        <channel0 ChannelName="Name1" />
        <channel1 ChannelName="Name2" />
        <channel2 ChannelName="Name3" />
    </Channels> 
</Data>

For some reason when I parse this from the xml file which has the above data in it, I only get it to print out channel0 Name1, it skips out the rest of the channels. Not sure if I'm missing something, or if I made a mistake. Could someone help me out? Thanks!

root = et.parse(path).getroot()    
for child in root:    
    for element in child:
        print element.tag, element.attrib['ChannelName']

1 Answer 1

1

I tried this in Python 3.6 and it printed out all three channels

import xml.etree.cElementTree as et

path = 'test.xml'

root = et.parse(path).getroot()    
for child in root:    
    for element in child:
        print(element.tag, element.attrib['ChannelName'])

output

channel0 Name1
channel1 Name2
channel2 Name3
Sign up to request clarification or add additional context in comments.

5 Comments

Oh good to know the code works... got to figure out what's wrong with the xml file then I guess. Thanks johnashu!
I think I figured out why it didn't work for me. I'm trying this through maya and maya uses python 2.7. Will have to figure out how to do the same thing without using elementTree
Ok this is bugging me now, for some reason the above code actually works if I run it through maya's script editor, but it doesn't work when I source the script.
maybe there is a version number difference. Check to see what version of element tree maya is using, then be sure to use the same version..use pip freeze to get the version requirements
I don't have pip installed where I work. Is there any other way to check the version?

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.