0

My XML looks something like: (Sorry if duplicate question, but I'm not very experienced with XML so I have a bit trouble with the terminology

<sometags>
    <Value>
        <Scalar unitGlobalDataRef="Unit_0" unit="None" xmlns="xxxxyyyy">20</Scalar> 
    </Value>
</sometags>

Using this code:

element = ET.parse(fileName)
root = element.getroot()
for subelement in root:
    if (subelement.tag == "{xxyy}Parameter"):
        for value in subelement:
            for subval in value:
                #Here is where it prints
                if (subval.tag == "{xxxxyyyy}Scalar"):
                    print subval.tag
                    print subval.text
                    print subval.tail
                    print subval.attrib

prints

{xxxxyyyy}Scalar
0


{'unitGlobalDataRef': 'Unit_0', 'unit': 'None'}

How can I get the value 20 out from the element?

4
  • What parser created the subval variable? Commented Jul 19, 2012 at 15:14
  • Edited with more of the code, hope it helps (can't post the whole code / XML without having to censor and rename a lot of stuff) Commented Jul 19, 2012 at 15:21
  • 2
    I'd expect it to be subval.text: ideone.com/1D2zj Commented Jul 19, 2012 at 15:25
  • 1
    By the way, with lxml you can do root.findall('.//{xxxxyyyy}Scalar') for a recursive search. Commented Jul 19, 2012 at 15:28

1 Answer 1

1

subval.text should containt the information you're seeking. Since you're getting a 0, it implies that your iterations may be wrong, and you're getting a different element than you think you are. This is further reinforced by the fact that your attributes don't match.

Sign up to request clarification or add additional context in comments.

1 Comment

You are correct, somehow I must have failed with the printouts or something. Using if subval.text != "0" : print subval.text I found the correct values

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.