2

I'm trying to write a program which will find all the lines containing the XML tag properties "name", "top" and "left" and will write value of this properties in console.

This is what I got so far:

from pathlib import Path
import os
import tempfile
import xml.etree.ElementTree as ET

xmlfile = "User_Registration.xml"

tree = ET.parse(xmlfile)
root = tree.getroot()

for elm in root.findall("./momentaryButton/name"):
    print(elm.attrib)'

It prints all attributes of just one tag (momentaryButton), but there are more tags - is there a way to extract 'name', 'top and 'left' from all tags at once?

I'm sure there is a fast way that I'm missing, and I hope that someone will help!
I'm pasting an XML file below:

<?xml version="1.0" encoding="UTF-8"?>
<gfx xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="Gfx-ME12.xsd">
    <displaySettings displayType="replace" securityCode="*" backColor="white" maximumTagUpdateRate="1" focusHighlightColor="lime" disableFocusHighlight="false" width="640" height="480" displayNumber="1" disableInitialInputFocus="false" startupMacro="" shutdownMacro="" useGradientStyle="false" endColor="teal" gradientStop="50" gradientDirection="gradientDirectionHorizontal" gradientShadingStyle="gradientHorizontalFromRight"/>
    <momentaryButton name="MomentaryPushButton1" height="50" width="100" left="10" top="50" visible="true" wallpaper="false" isReferenceObject="false" audio="true" backStyle="solid" borderStyle="raised" borderUsesBackColor="true" borderWidth="4" buttonAction="normallyOpen" description="" holdTime="250" highlightColor="lime" horizontalMargin="0" verticalMargin="0" shape="rectangle" touch="true" currentStateId="1" captionOnBorder="false">
        <states>
            <state stateId="Error" backColor="navy" borderColor="navy" patternColor="white" patternStyle="none" blink="false" endColor="white" gradientStop="50" gradientDirection="gradientDirectionHorizontal" gradientShadingStyle="gradientHorizontalFromRight">
                <caption fontFamily="Arial Unicode MS" fontSize="10" bold="false" italic="false" underline="false" strikethrough="false" caption="Error" color="white" backColor="navy" backStyle="transparent" alignment="middleCenter" wordWrap="true" blink="false"/>
                <imageSettings imageName="" alignment="middleCenter" backStyle="transparent" color="white" backColor="navy" scaled="false" blink="false"/>
            </state>
            <state stateId="0" backColor="navy" borderColor="navy" patternColor="white" patternStyle="none" blink="false" endColor="white" gradientStop="50" gradientDirection="gradientDirectionHorizontal" gradientShadingStyle="gradientHorizontalFromRight">
                <caption fontFamily="Arial Unicode MS" fontSize="10" bold="false" italic="false" underline="false" strikethrough="false" caption="Kopiuj ostatnie dane z czytnika" color="white" backColor="navy" backStyle="transparent" alignment="middleCenter" wordWrap="true" blink="false"/>
                <imageSettings imageName="" alignment="middleCenter" backStyle="transparent" color="white" backColor="navy" scaled="false" blink="false"/>
            </state>
            <state stateId="1" backColor="navy" borderColor="navy" patternColor="white" patternStyle="none" blink="false" endColor="white" gradientStop="50" gradientDirection="gradientDirectionHorizontal" gradientShadingStyle="gradientHorizontalFromRight">
                <caption fontFamily="Arial Unicode MS" fontSize="10" bold="false" italic="false" underline="false" strikethrough="false" caption="Kopiuj ostatnie dane z czytnika" color="white" backColor="navy" backStyle="transparent" alignment="middleCenter" wordWrap="true" blink="false"/>
                <imageSettings imageName="" alignment="middleCenter" backStyle="transparent" color="white" backColor="navy" scaled="false" blink="false"/>
            </state>
        </states>
        <connections>
            <connection name="Value" expression="{[PLC]Copy_Card_Data}"/>
            <connection name="Indicator" expression="{[PLC]HMI_Void}"/>
        </connections>
    </momentaryButton>
    <stringInputEnable name="StringInputEnable1" height="50" width="100" left="10" top="170" visible="true" wallpaper="false" isReferenceObject="false" audio="true" backColor="navy" backStyle="solid" borderStyle="raised" borderUsesBackColor="true" borderWidth="4" description="" highlightColor="lime" borderColor="navy" patternColor="white" patternStyle="none" horizontalMargin="0" verticalMargin="0" shape="rectangle" touch="true" blink="false" enterKeyControlDelay="400" enterKeyHandshakeTime="4" enterKeyHoldTime="250" handshakeReset="nonZeroValue" keyNavigation="false" maskScratchpad="false" takeFocusOnPress="false" numberOfInputCharacters="8" fillCharacter="null" stringPopup="keyboard" captionOnBorder="false" endColor="white" gradientStop="50" gradientDirection="gradientDirectionHorizontal" gradientShadingStyle="gradientHorizontalFromRight" RequireElectronicSignature="false" AllowBlankComment="false" RequireReAuthentication="false" RequireCounterSignature="false" AuthorizedGroup="Windows Administrators" ESDomainNameVisible="false" ESDomainNameType="ESDomainNameConstant" ESDomainName="" VariableDomainName="" ESDomainNameDisable="false">
        <caption fontFamily="Arial Unicode MS" fontSize="10" bold="false" italic="false" underline="false" strikethrough="false" caption="Wpisz dane ręcznie" color="white" backColor="navy" backStyle="transparent" alignment="middleCenter" wordWrap="true" blink="false"/>
        <imageSettings imageName="" alignment="middleCenter" backStyle="transparent" color="white" backColor="navy" scaled="false" blink="false"/>
        <connections>
            <connection name="Value" expression="{[PLC]Register_ID}"/>
        </connections>
    </stringInputEnable>
    <text name="Text1" height="20" width="81" left="10" top="10" visible="true" wallpaper="false" isReferenceObject="false" backStyle="transparent" backColor="white" foreColor="black" wordWrap="true" sizeToFit="true" alignment="middleCenter" fontFamily="Arial Unicode MS" charHeight="20" charWidth="6" bold="false" italic="false" underline="false" strikethrough="false" caption="Zalogowany:"/>
    <text name="Text2" height="20" width="51" left="240" top="10" visible="true" wallpaper="false" isReferenceObject="false" backStyle="transparent" backColor="white" foreColor="black" wordWrap="true" sizeToFit="true" alignment="middleCenter" fontFamily="Arial Unicode MS" charHeight="20" charWidth="6" bold="false" italic="false" underline="false" strikethrough="false" caption="Poziom:"/>
    <text name="Text3" height="20" width="48" left="330" top="10" visible="true" wallpaper="false" isReferenceObject="false" backStyle="transparent" backColor="white" foreColor="black" wordWrap="true" sizeToFit="true" alignment="middleCenter" fontFamily="Arial Unicode MS" charHeight="20" charWidth="6" bold="false" italic="false" underline="false" strikethrough="false" caption="Nazwa:"/>
    <text name="Text4" height="20" width="111" left="390" top="10" visible="true" wallpaper="false" isReferenceObject="false" backStyle="transparent" backColor="white" foreColor="black" wordWrap="true" sizeToFit="true" alignment="middleCenter" fontFamily="Arial Unicode MS" charHeight="20" charWidth="6" bold="false" italic="false" underline="false" strikethrough="false" caption="Brak użytkownika">
        <animations>
            <animateVisibility expression="({[PLC]User_Level} == 0) AND ({[PLC]Unauth_User_Has_Lvl_1} == 0)" expressionTrueState="visible"/>
        </animations>
    </text>
    <text name="Text5" height="20" width="9" left="295" top="10" visible="true" wallpaper="false" isReferenceObject="false" backStyle="transparent" backColor="white" foreColor="black" wordWrap="true" sizeToFit="true" alignment="middleCenter" fontFamily="Arial Unicode MS" charHeight="20" charWidth="6" bold="false" italic="false" underline="false" strikethrough="false" caption="/*N:1 {[PLC]User_Level} NOFILL DP:0*/"/>
    <text name="Text7" height="20" width="27" left="100" top="10" visible="true" wallpaper="false" isReferenceObject="false" backStyle="transparent" backColor="white" foreColor="black" wordWrap="true" sizeToFit="true" alignment="middleCenter" fontFamily="Arial Unicode MS" charHeight="20" charWidth="6" bold="false" italic="false" underline="false" strikethrough="false" caption="/*S:0 {[PLC]User_Name}*/">
        <animations>
            <animateVisibility expression="({[PLC]User_Level} == 0) AND ({[PLC]Unauth_User_Has_Lvl_1} == 0)" expressionTrueState="invisible"/>
        </animations>
    </text>
    <text name="Text6" height="20" width="27" left="390" top="10" visible="true" wallpaper="false" isReferenceObject="false" backStyle="transparent" backColor="white" foreColor="black" wordWrap="true" sizeToFit="true" alignment="middleCenter" fontFamily="Arial Unicode MS" charHeight="20" charWidth="6" bold="false" italic="false" underline="false" strikethrough="false" caption="/*S:0 [PLC]Access_Level_Name*/">
        <animations>
            <animateVisibility expression="({[PLC]User_Level} == 0) AND ({[PLC]Unauth_User_Has_Lvl_1} == 0)" expressionTrueState="invisible"/>
        </animations>
    </text>
    <text name="Text8" height="20" width="97" left="100" top="10" visible="true" wallpaper="false" isReferenceObject="false" backStyle="transparent" backColor="white" foreColor="black" wordWrap="true" sizeToFit="true" alignment="middleCenter" fontFamily="Arial Unicode MS" charHeight="20" charWidth="6" bold="false" italic="false" underline="false" strikethrough="false" caption="Niezalogowany">
        <animations>
            <animateVisibility expression="({[PLC]User_Level} == 0) AND ({[PLC]Unauth_User_Has_Lvl_1} == 0)" expressionTrueState="visible"/>
        </animations>
    </text>
    <stringDisplay name="StringDisplay1" height="50" width="100" left="120" top="110" visible="true" wallpaper="false" isReferenceObject="false" backColor="navy" backStyle="solid" borderColor="navy" borderStyle="raisedInset" borderUsesBackColor="true" borderWidth="4" foreColor="white" alignment="middleCenter" blink="false" wordWrap="true" patternColor="white" patternStyle="none" description="" fontFamily="Arial Unicode MS" fontSize="10" bold="false" italic="false" underline="false" strikethrough="false" endColor="white" gradientStop="50" gradientDirection="gradientDirectionHorizontal" gradientShadingStyle="gradientHorizontalFromRight">
        <connections>
            <connection name="Value" expression="{[PLC]Register_ID}"/>
        </connections>
    </stringDisplay>
    <returnToButton name="ReturntoDisplayButton4" height="50" width="100" left="530" top="420" visible="true" wallpaper="false" isReferenceObject="false" audio="true" backColor="navy" backStyle="solid" borderStyle="raised" borderUsesBackColor="true" borderWidth="4" description="" highlightColor="lime" borderColor="navy" patternColor="white" patternStyle="none" horizontalMargin="0" verticalMargin="0" shape="rectangle" touch="true" blink="false" captionOnBorder="false" endColor="white" gradientStop="50" gradientDirection="gradientDirectionHorizontal" gradientShadingStyle="gradientHorizontalFromRight">
        <caption fontFamily="Arial Unicode MS" fontSize="10" bold="false" italic="false" underline="false" strikethrough="false" caption="Wyjście" color="white" backColor="navy" backStyle="transparent" alignment="middleCenter" wordWrap="true" blink="false"/>
        <imageSettings imageName="" alignment="middleCenter" backStyle="transparent" color="white" backColor="navy" scaled="false" blink="false"/>
    </returnToButton>
    <stringInputEnable name="StringInputEnable2" height="50" width="100" left="230" top="80" visible="true" wallpaper="false" isReferenceObject="false" audio="true" backColor="navy" backStyle="solid" borderStyle="raised" borderUsesBackColor="true" borderWidth="4" description="" highlightColor="lime" borderColor="navy" patternColor="white" patternStyle="none" horizontalMargin="0" verticalMargin="0" shape="rectangle" touch="true" blink="false" enterKeyControlDelay="400" enterKeyHandshakeTime="4" enterKeyHoldTime="250" handshakeReset="nonZeroValue" keyNavigation="false" maskScratchpad="false" takeFocusOnPress="false" numberOfInputCharacters="8" fillCharacter="null" stringPopup="keyboard" captionOnBorder="false" endColor="white" gradientStop="50" gradientDirection="gradientDirectionHorizontal" gradientShadingStyle="gradientHorizontalFromRight" RequireElectronicSignature="false" AllowBlankComment="false" RequireReAuthentication="false" RequireCounterSignature="false" AuthorizedGroup="" ESDomainNameVisible="false" ESDomainNameType="ESDomainNameConstant" ESDomainName="" VariableDomainName="" ESDomainNameDisable="false">
        <caption fontFamily="Arial Unicode MS" fontSize="10" bold="false" italic="false" underline="false" strikethrough="false" caption="Zalogowany: /*S:0 {[PLC]PV_User_Name}*/" color="white" backColor="navy" backStyle="transparent" alignment="middleCenter" wordWrap="true" blink="false"/>
        <imageSettings imageName="" alignment="middleCenter" backStyle="transparent" color="white" backColor="navy" scaled="false" blink="false"/>
        <connections>
            <connection name="Value" expression="{[PLC]User_Name_To_Change}"/>
        </connections>
    </stringInputEnable>
    <numericInputEnable name="NumericInputEnable1" height="50" width="100" left="340" top="80" visible="true" wallpaper="false" isReferenceObject="false" audio="true" backColor="navy" backStyle="solid" borderStyle="raised" borderUsesBackColor="true" borderWidth="4" description="" highlightColor="lime" borderColor="navy" patternColor="white" patternStyle="none" horizontalMargin="0" verticalMargin="0" shape="rectangle" touch="true" blink="false" enterKeyControlDelay="400" enterKeyHandshakeTime="4" enterKeyHoldTime="250" handshakeReset="nonZeroValue" keyNavigation="true" decimalPoint="implicit" numericPopup="keypad" rampValue="0" useVariableMinMax="false" takeFocusOnPress="false" minValue="0" maxValue="255" captionOnBorder="false" endColor="white" gradientStop="50" gradientDirection="gradientDirectionHorizontal" gradientShadingStyle="gradientHorizontalFromRight" RequireElectronicSignature="false" AllowBlankComment="false" RequireReAuthentication="false" RequireCounterSignature="false" AuthorizedGroup="" ESDomainNameVisible="false" ESDomainNameType="ESDomainNameConstant" ESDomainName="" VariableDomainName="" ESDomainNameDisable="false">
        <caption fontFamily="Arial Unicode MS" fontSize="10" bold="false" italic="false" underline="false" strikethrough="false" caption="Poziom: /*N:1 {[PLC]PV_User_Level} NOFILL DP:0*/" color="white" backColor="navy" backStyle="transparent" alignment="middleCenter" wordWrap="true" blink="false"/>
        <imageSettings imageName="" alignment="middleCenter" backStyle="transparent" color="white" backColor="navy" scaled="false" blink="false"/>
        <connections>
            <connection name="Value" expression="{[PLC]User_Level_To_Change}"/>
        </connections>
    </numericInputEnable>
    <numericDisplay name="NumericDisplay2" height="50" width="100" left="340" top="140" visible="true" wallpaper="false" isReferenceObject="false" backColor="navy" backStyle="solid" borderColor="navy" borderStyle="raisedInset" borderUsesBackColor="true" borderWidth="8" foreColor="white" alignment="middleCenter" blink="false" patternColor="white" patternStyle="none" description="" decimalPlaces="0" numberOfDigits="5" fillLeftWith="none" fontFamily="Arial Unicode MS" fontSize="10" bold="false" italic="false" underline="false" strikethrough="false" endColor="white" gradientStop="50" gradientDirection="gradientDirectionHorizontal" gradientShadingStyle="gradientHorizontalFromRight">
        <connections>
            <connection name="Value" expression="{[PLC]User_Level_To_Change}"/>
        </connections>
    </numericDisplay>
    <stringDisplay name="StringDisplay3" height="50" width="100" left="230" top="140" visible="true" wallpaper="false" isReferenceObject="false" backColor="navy" backStyle="solid" borderColor="navy" borderStyle="raisedInset" borderUsesBackColor="true" borderWidth="8" foreColor="white" alignment="middleCenter" blink="false" wordWrap="true" patternColor="white" patternStyle="none" description="" fontFamily="Arial Unicode MS" fontSize="10" bold="false" italic="false" underline="false" strikethrough="false" endColor="white" gradientStop="50" gradientDirection="gradientDirectionHorizontal" gradientShadingStyle="gradientHorizontalFromRight">
        <connections>
            <connection name="Value" expression="{[PLC]User_Name_To_Change}"/>
        </connections>
    </stringDisplay>
    <momentaryButton name="MomentaryPushButton2" height="50" width="100" left="450" top="80" visible="true" wallpaper="false" isReferenceObject="false" audio="true" backStyle="solid" borderStyle="raised" borderUsesBackColor="true" borderWidth="4" buttonAction="normallyOpen" description="" holdTime="250" highlightColor="lime" horizontalMargin="0" verticalMargin="0" shape="rectangle" touch="true" currentStateId="0" captionOnBorder="false">
        <states>
            <state stateId="Error" backColor="navy" borderColor="navy" patternColor="white" patternStyle="none" blink="false" endColor="white" gradientStop="50" gradientDirection="gradientDirectionHorizontal" gradientShadingStyle="gradientHorizontalFromRight">
                <caption fontFamily="Arial Unicode MS" fontSize="10" bold="false" italic="false" underline="false" strikethrough="false" caption="Error" color="white" backColor="navy" backStyle="transparent" alignment="middleCenter" wordWrap="true" blink="false"/>
                <imageSettings imageName="" alignment="middleCenter" backStyle="transparent" color="white" backColor="navy" scaled="false" blink="false"/>
            </state>
            <state stateId="0" backColor="lime" borderColor="lime" patternColor="white" patternStyle="none" blink="false" endColor="white" gradientStop="50" gradientDirection="gradientDirectionHorizontal" gradientShadingStyle="gradientHorizontalFromRight">
                <caption fontFamily="Arial Unicode MS" fontSize="10" bold="false" italic="false" underline="false" strikethrough="false" caption="Zatwierdź zmiany" color="black" backColor="navy" backStyle="transparent" alignment="middleCenter" wordWrap="true" blink="false"/>
                <imageSettings imageName="" alignment="middleCenter" backStyle="transparent" color="white" backColor="navy" scaled="false" blink="false"/>
            </state>
            <state stateId="1" backColor="lime" borderColor="lime" patternColor="white" patternStyle="none" blink="false" endColor="white" gradientStop="50" gradientDirection="gradientDirectionHorizontal" gradientShadingStyle="gradientHorizontalFromRight">
                <caption fontFamily="Arial Unicode MS" fontSize="10" bold="false" italic="false" underline="false" strikethrough="false" caption="Zatwierdź zmiany" color="black" backColor="navy" backStyle="transparent" alignment="middleCenter" wordWrap="true" blink="false"/>
                <imageSettings imageName="" alignment="middleCenter" backStyle="transparent" color="white" backColor="navy" scaled="false" blink="false"/>
            </state>
        </states>
        <connections>
            <connection name="Value" expression="{[PLC]Apply_Changes}"/>
            <connection name="Indicator" expression="{[PLC]HMI_Void}"/>
        </connections>
    </momentaryButton>
    <momentaryButton name="MomentaryPushButton3" height="50" width="100" left="450" top="140" visible="true" wallpaper="false" isReferenceObject="false" audio="true" backStyle="solid" borderStyle="raised" borderUsesBackColor="true" borderWidth="4" buttonAction="normallyOpen" description="" holdTime="250" highlightColor="lime" horizontalMargin="0" verticalMargin="0" shape="rectangle" touch="true" currentStateId="0" captionOnBorder="false">
        <states>
            <state stateId="Error" backColor="navy" borderColor="navy" patternColor="white" patternStyle="none" blink="false" endColor="white" gradientStop="50" gradientDirection="gradientDirectionHorizontal" gradientShadingStyle="gradientHorizontalFromRight">
                <caption fontFamily="Arial Unicode MS" fontSize="10" bold="false" italic="false" underline="false" strikethrough="false" caption="Error" color="white" backColor="navy" backStyle="transparent" alignment="middleCenter" wordWrap="true" blink="false"/>
                <imageSettings imageName="" alignment="middleCenter" backStyle="transparent" color="white" backColor="navy" scaled="false" blink="false"/>
            </state>
            <state stateId="0" backColor="red" borderColor="red" patternColor="white" patternStyle="none" blink="false" endColor="white" gradientStop="50" gradientDirection="gradientDirectionHorizontal" gradientShadingStyle="gradientHorizontalFromRight">
                <caption fontFamily="Arial Unicode MS" fontSize="10" bold="false" italic="false" underline="false" strikethrough="false" caption="Cofnij zmiany" color="white" backColor="navy" backStyle="transparent" alignment="middleCenter" wordWrap="true" blink="false"/>
                <imageSettings imageName="" alignment="middleCenter" backStyle="transparent" color="white" backColor="navy" scaled="false" blink="false"/>
            </state>
            <state stateId="1" backColor="red" borderColor="red" patternColor="white" patternStyle="none" blink="false" endColor="white" gradientStop="50" gradientDirection="gradientDirectionHorizontal" gradientShadingStyle="gradientHorizontalFromRight">
                <caption fontFamily="Arial Unicode MS" fontSize="10" bold="false" italic="false" underline="false" strikethrough="false" caption="Cofnij zmiany" color="white" backColor="navy" backStyle="transparent" alignment="middleCenter" wordWrap="true" blink="false"/>
                <imageSettings imageName="" alignment="middleCenter" backStyle="transparent" color="white" backColor="navy" scaled="false" blink="false"/>
            </state>
        </states>
        <connections>
            <connection name="Value" expression="{[PLC]Discard_Changes}"/>
            <connection name="Indicator" expression="{[PLC]HMI_Void}"/>
        </connections>
    </momentaryButton>
    <momentaryButton name="MomentaryPushButton4" height="50" width="100" left="10" top="110" visible="true" wallpaper="false" isReferenceObject="false" audio="true" backStyle="solid" borderStyle="raised" borderUsesBackColor="true" borderWidth="4" buttonAction="normallyOpen" description="" holdTime="250" highlightColor="lime" horizontalMargin="0" verticalMargin="0" shape="rectangle" touch="true" currentStateId="1" captionOnBorder="false">
        <states>
            <state stateId="Error" backColor="navy" borderColor="navy" patternColor="white" patternStyle="none" blink="false" endColor="white" gradientStop="50" gradientDirection="gradientDirectionHorizontal" gradientShadingStyle="gradientHorizontalFromRight">
                <caption fontFamily="Arial Unicode MS" fontSize="10" bold="false" italic="false" underline="false" strikethrough="false" caption="Error" color="white" backColor="navy" backStyle="transparent" alignment="middleCenter" wordWrap="true" blink="false"/>
                <imageSettings imageName="" alignment="middleCenter" backStyle="transparent" color="white" backColor="navy" scaled="false" blink="false"/>
            </state>
            <state stateId="0" backColor="navy" borderColor="navy" patternColor="white" patternStyle="none" blink="false" endColor="white" gradientStop="50" gradientDirection="gradientDirectionHorizontal" gradientShadingStyle="gradientHorizontalFromRight">
                <caption fontFamily="Arial Unicode MS" fontSize="10" bold="false" italic="false" underline="false" strikethrough="false" caption="Skanuj nową kartę" color="white" backColor="navy" backStyle="transparent" alignment="middleCenter" wordWrap="true" blink="false"/>
                <imageSettings imageName="" alignment="middleCenter" backStyle="transparent" color="white" backColor="navy" scaled="false" blink="false"/>
            </state>
            <state stateId="1" backColor="navy" borderColor="navy" patternColor="white" patternStyle="none" blink="false" endColor="white" gradientStop="50" gradientDirection="gradientDirectionHorizontal" gradientShadingStyle="gradientHorizontalFromRight">
                <caption fontFamily="Arial Unicode MS" fontSize="10" bold="false" italic="false" underline="false" strikethrough="false" caption="Skanuj nową kartę" color="white" backColor="navy" backStyle="transparent" alignment="middleCenter" wordWrap="true" blink="false"/>
                <imageSettings imageName="" alignment="middleCenter" backStyle="transparent" color="white" backColor="navy" scaled="false" blink="false"/>
            </state>
        </states>
        <connections>
            <connection name="Value" expression="{[PLC]Scan_New_Data}"/>
            <connection name="Indicator" expression="{[PLC]HMI_Void}"/>
        </connections>
    </momentaryButton>
</gfx>
zx485
  • 28,498
  • 28
  • 50
  • 59
Mixhalo
  • 33
  • 1
  • 5
  • Does this answer your question? [python elementtree - how to find all elements in xml with certain attribute](https://stackoverflow.com/questions/55906438/python-elementtree-how-to-find-all-elements-in-xml-with-certain-attribute) – eandklahn Feb 23 '22 at 18:43

1 Answers1

1

Try changing your for loop to

for elm in doc.findall("./momentaryButton"):
    print(elm.attrib['name'],elm.attrib['top'],elm.attrib['left'])

Output should be

MomentaryPushButton1 50 10
MomentaryPushButton2 80 450
MomentaryPushButton3 140 450
MomentaryPushButton4 110 10
Jack Fleeting
  • 24,385
  • 6
  • 23
  • 45
  • Thank You very much! Yes it works, but i need to manualy look for tags that contain "name" "top" and "left" and there is a lot of them. Is there a way to go thought a whole XML file and print ALL tags that contain elements "name" "top" and "left"? I would help a lot, if not i have to go manualy like: for elm in root.findall("./"): print(elm.tag) to see all tags and then manualy extract attributes from them. – Mixhalo Feb 23 '22 at 20:28
  • @Mixhalo - Well, that's a different question really and you probably should post it as a new question. When you do, make sure you clarify **exactly** what your search conditions are: to use a more price terminology, are you looking to print out the attribute values of ALL elements which have these three specific attributes or of elements which have any one of the three? Make sure you also add your exact expected output. – Jack Fleeting Feb 23 '22 at 20:59
  • Thanks for letting me know, will do as you say. Anwsering to Your question, I'm looking for ALL elements witch have these three specifiv attributes. Thanks once again! – Mixhalo Feb 23 '22 at 21:07
  • @Mixhalo You're welcome. I'll keep an eye out for the new question, too. If we're done, don't forget to accept the answer, please. – Jack Fleeting Feb 23 '22 at 21:48
  • I tried, but it says i need to have at least 15 reputation to cast a vote, or am i doing smth wrong? – Mixhalo Feb 23 '22 at 21:50
  • @Mixhalo You tried to upvote the question (which you couldn't then; you can now), instead of accepting it :); [take a look here](https://stackoverflow.com/help/someone-answers). – Jack Fleeting Feb 23 '22 at 22:03