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>
CodePudding user response:
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