简体   繁体   中英

Get Element using elementtree python

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM