0

I have below xml structure with sub/child element

<Sales>
    <SalesContacts>
        <Enty>
            <ns0:Active>true</ns0:Active>
            <ns0:Contact>
                <ns1:Person>
                    <ns1:FirstName>myname</ns1:FirstName>
                    <ns1:LastName>seond name</ns1:LastName>
                </ns1:Person>
                <ns1:EmailAddress1>abcr@gmail.com</ns1:EmailAddress1>
                <ns1:EmailAddress2>abc2@sgmail.com</ns1:EmailAddress2>
                <ns1:FaxPhone>0002222</ns1:FaxPhone>
                <ns1:HomePhone>000222</ns1:HomePhone>
                <ns1:Person>
                    <ns1:DisplayName>My Name</ns1:DisplayName>
                </ns1:Person>
                <ns1:Address>
                    <ns2:AddressLine1>my_address</ns2:AddressLine1>
                    <ns2:AddressType>
                        <ns2:Code>home</ns2:Code>
                        <ns2:Description>Home</ns2:Description>
                        <ns2:Name>Home</ns2:Name>
                    </ns2:AddressType>
                    <ns2:City>SAINT CHARLES</ns2:City>
                    <ns2:Country>
                        <ns2:Code>Uk</ns2:Code>
                        <ns2:Description>uk</ns2:Description>
                        <ns2:Name>United States</ns2:Name>
                    </ns2:Country>
                    <ns2:County>Charles</ns2:County>
                    <ns2:PostalCode>000222</ns2:PostalCode>
                    <ns2:PublicID>dc:1000</ns2:PublicID>
                    <ns2:Retired>false</ns2:Retired>
                    <ns2:State>MD</ns2:State>
                    <ns2:UpdateTime>mytime</ns2:UpdateTime>
                </ns1:Address>
                <ns1:PrimaryPhone>
                    <ns1:Code>home</ns1:Code>
                    <ns1:Description>Home</ns1:Description>
                    <ns1:Name>Home</ns1:Name>
                </ns1:PrimaryPhone>
                <ns1:PublicID>dc1111</ns1:PublicID>
                <ns1:Retired>false</ns1:Retired>
                <ns1:UpdateTime>mytime</ns1:UpdateTime>
            </ns0:Contact>
            <ns0:PublicID>pc:630121</ns0:PublicID>
            <ns0:Roles>
                <ns0:Enty>
                    <ns3:PublicID>dc:1231231</ns3:PublicID>
                    <ns3:Subtype>
                        <ns3:Code>SalesHolder</ns3:Code>
                        <ns3:Description>SalesHolder</ns3:Description>
                        <ns3:Name>SalesHolder</ns3:Name>
                    </ns3:Subtype>
                    <ns3:UpdateTime>Mytime</ns3:UpdateTime>
                </ns0:Enty>
                <ns0:Enty>
                    <ns3:PublicID>dc:12312321</ns3:PublicID>
                    <ns3:Subtype>
                        <ns3:Code>mybusiness</ns3:Code>
                        <ns3:Description>mybusiness</ns3:Description>
                        <ns3:Name>mybusiness</ns3:Name>
                    </ns3:Subtype>
                    <ns3:UpdateTime>mytime</ns3:UpdateTime>
                </ns0:Enty>
            </ns0:Roles>
            <ns0:UpdateTime>mytime</ns0:UpdateTime>
        </Enty>
    </SalesContacts>
</Sales>

now i need to fetch the sub element 'Roles/Enty' from parent 'Enty' i tried with below way

test_root = etree.fromstring(payload_xml_obj_wf).getroottree()

result=test_root.find('Enty').attrib.get('Roles')
print(result)

But iam getting the result says

'NoneType' object has no attribute 'attrib'

#Expected: i need to get the count of 'Enty' inside the 'Roles'- . Here in the case, it will be two

     <ns0:Roles>
         <ns0:Enty><-><ns0:Enty>
         <ns0:Enty><-><ns0:Enty>
     <ns0:Roles>

Appreciated if there any solution to resolve this?

Thanks

cloud_hari
  • 147
  • 1
  • 8
  • The "xml structure" is ill-formed; i.e. it is not XML. – mzjn Feb 04 '21 at 17:59
  • HI @mzjn i didn't put the full xml structure, because of some confidential reason, but it doesn't mean, whatever error i was getting due to 'ill-formated'.. i just want to know how to get the child element count, here in the case ''Enty' inside the 'Roles' ? Generally how to get the sub element /child element count from the xml? – cloud_hari Feb 05 '21 at 05:15
  • 1
    Please make the effort of submitting a well-formed sample document. That is really not too much to ask. The `ns0` prefix indicates that a namespace is involved, but the prefix is not properly declared. – mzjn Feb 05 '21 at 05:36
  • Hi @mzjn i have updated with the xml structure.. please have a look.. THanks – cloud_hari Feb 05 '21 at 08:09
  • 1
    Sorry, but this is not an improvement. You have added even more undefined namespace prefixes. See https://stackoverflow.com/q/10393435/407651 – mzjn Feb 05 '21 at 08:17

0 Answers0