1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. Anuncie Aqui
    Anuncie aqui você Também: fdantas@4each.com.br

[Python] Python ElementTree iter() method using XPath

Discussão em 'Python' iniciado por Stack, Abril 13, 2025.

  1. Stack

    Stack Membro Participativo

    I have the following XML element:

    <Content>
    <Controller Use="Context" Name="Base_Project_Maximum_Connections">
    <DataTypes Use="Context">
    <DataType Use="Target" Name="cs_dint" Family="NoFamily" Class="User">
    <Members>
    <Member Name="Status" DataType="CONNECTION_STATUS" Dimension="0" Radix="NullType" Hidden="false" ExternalAccess="Read/Write" />
    <Member Name="Data" DataType="DINT" Dimension="0" Radix="Decimal" Hidden="false" ExternalAccess="Read/Write" />
    </Members>
    </DataType>
    </DataTypes>
    </Controller>
    </Content>


    I am trying to find all DataType elements that have a Name of "cs_dint" using the following code:

    def GetDataTypeFromElement(root, typeName = None):
    dataTypeIter = root.iter("DataType")
    for dataTypeElement in dataTypeIter:
    print ("Found a data type element")
    print ("Finished with first iteration")

    dataTypeIter = root.iter("DataType[@Name = 'cs_dint']")
    for dataTypeElement in dataTypeIter:
    print ("Found a data type element")
    print ("Finished with second iteration")

    return None


    The first call to iter(), where I merely look for DataType elements, works. The second, where I try to specify the name, doesn't. Why not?

    Continue reading...

Compartilhe esta Página