0

I am getting an xml response like below

<entitlement>
  <externalId></externalId>
  <entitlementAsWhole>false</entitlementAsWhole>
  <eId>1c7fd51c-8f12-46e8-a4b7-f1f9c614df82</eId>
 <entitlementType>PARENT</entitlementType>
 <linkedEntId/>
   <product>
    <productIdentifier>
      <prdExternalId></prdExternalId>
      <productId>7</productId>
      <productNameVersion>
        <productName>test2_porduct</productName>
        <productVersion>1.0.0</productVersion>
      </productNameVersion>
    </productIdentifier>
    <feature>
      <featureIdentifier>
        <ftrExternalId></ftrExternalId>
        <featureId>7</featureId>
        <featureIdentity>null</featureIdentity>
        <ftrNameVersion>
          <featureName>test2_feature</featureName>
          <featureVersion>1.0.0</featureVersion>
        </ftrNameVersion>
      </featureIdentifier>
      <activationAttributes>
    <attributeGroup groupName="LOCKING">
      <attribute>
        <attributeName>CLIENT_1_CRITERIA</attributeName>
        <attributeValue>4</attributeValue>
        <readOnly>true</readOnly>
        <mandatory>false</mandatory>
      </attribute>
      <attribute>
        <attributeName>CLIENT_1_INFO</attributeName>
        <attributeValue></attributeValue>
        <readOnly>false</readOnly>
        <mandatory>true</mandatory>
      </attribute>
    </attributeGroup>
  </activationAttributes>
  <entitlementItemAttributes/>
</Item>
 </productKey>
 <entitlementAttributes/>
</entitlement>

And I want to get xml from above xml having only activation attribute feild some thing like below:-

<activationAttributes>
    <attributeGroup groupName="LOCKING">
      <attribute>
        <attributeName>CLIENT_1_CRITERIA</attributeName>
        <attributeValue>4</attributeValue>
        <readOnly>true</readOnly>
        <mandatory>false</mandatory>
      </attribute>
      <attribute>
        <attributeName>CLIENT_1_INFO</attributeName>
        <attributeValue></attributeValue>
        <readOnly>false</readOnly>
        <mandatory>true</mandatory>
      </attribute>
    </attributeGroup>
  </activationAttributes>

how can I do it I tried like below where resp.txt contains the orginal xml however it didn't help

activation_attribute = et.fromstring(resp.text).findall('activationAttributes')
DavidS
  • 276
  • 1
  • 17
Ruchir Bharadwaj
  • 1,132
  • 4
  • 15
  • 31

1 Answers1

1

Use find() to get only one element, and then use tostring() to get raw XML representation of the selected element :

from xml.etree import cElementTree as et

.....

root = et.fromstring(resp.text)
activation_attribute = root.find('.//activationAttributes')
print et.tostring(activation_attribute)
har07
  • 88,338
  • 12
  • 84
  • 137