1

I am trying to access and modify a tag deep with in the hierarchy of an XML . I have used quite a few options to reach it . Please help me accessing and modifying the tag . Here is my XML :

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:cre="http://www.code.com/abc/V1/createCase">
   <soapenv:Header><wsse:Security xmlns:wsse="http://docs.oasis-open.org/2" xmlns:wsu="http://docs.oasis-open.org/a.xsd"></wsse:Security>
   </soapenv:Header>
   <soapenv:Body xmlns:wsu="http://docs.oasis-open.org/30.xsd" wsu:Id="id-14">
      <cre:createCase>
         <cre:Request>
            <cre:ServiceAttributesGrp>
               <cre:MinorVer>?</cre:MinorVer>
            </cre:ServiceAttributesGrp>
            <cre:CreateCaseReqGrp>
               <cre:Language>English</cre:Language>
               <cre:CustFirstNm>Issue</cre:CustFirstNm>
               <cre:CustLastNm>Detection</cre:CustLastNm>
               <cre:AddlDynInfoGrp>
                  <cre:AddlDynInfo>
                           <cre:FieldNm>TM3</cre:FieldNm>
                           <cre:FieldVal></cre:FieldVal>
                  </cre:AddlDynInfo>
                  <cre:AddlDynInfo>
                           <cre:FieldNm>PM417</cre:FieldNm>
                           <cre:FieldVal>Not Defined</cre:FieldVal>
                  </cre:AddlDynInfo>
               </cre:AddlDynInfoGrp>
               <cre:CreateCriteriasGrp>
                  <cre:CreateCriterias>
                     <cre:CriteriaNm>CriticalReqDtlValidationReqd</cre:CriteriaNm>
                  </cre:CreateCriterias>
               </cre:CreateCriteriasGrp>
            </cre:CreateCaseReqGrp>
         </cre:Request>
      </cre:createCase>
   </soapenv:Body>
</soapenv:Envelope>

I have to access and modify the value of "FieldVal" tag in "AddlDynInfo" Tag , where the corresponding value of "FieldNm" tag value is "PM417" (since there are two occurances of "AddlDynInfo" tag . As of now , I am stuck on the parent tag only , as I could not access it :

tree = etree.parse(template_xml)
root = tree.getroot()
for msgBody in root[1]:
  for createCase in msgBody:
    for request in createCase:
     print request
     for CreateCaseReqGrp in request.findall('{cre}CreateCaseReqGrp',namespaces=root.nsmap):
     print CreateCaseReqGrp

1 Answer 1

1

Defined namespaces and XPaths make this quite easy. Your case would be something like this:

ns = {
    'soapenv': 'http://schemas.xmlsoap.org/soap/envelope/',
    'cre': 'http://www.code.com/abc/V1/createCase'
}

for casereq in root.xpath(
    'soapenv:Body/cre:createCase/cre:Request/'
    'cre:CreateCaseReqGrp/cre:AddlDynInfoGrp/cre:AddlDynInfo', namespaces=ns):
    print casereq.xpath('cre:FieldNm/text()', namespaces=ns)
    print casereq.xpath('cre:FieldVal/text()', namespaces=ns)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.