使用 lxml 打印皂体数据

Print Soap Body data using lxml

我有一个追随者XML。我需要将 Soap 请求中的整个主体 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>

截至目前,我正在尝试以下列方式打印,但我无法:

ns = {'cre': 'http://www.americanexpress.com/worldservice/CLIC/CaseManagementService/V1/createCase' , 'soapenv':'http://schemas.xmlsoap.org/soap/envelope/'}
     tree = etree.parse(template_xml)
     root = tree.getroot()
     for bodytag in root.xpath('soapenv:Body/cre:createCase',namespaces=ns):
       print bodytag
       datalevel = etree.XPathEvaluator(bodytag,namespaces=ns)
       print datalevel('cre:createCase').text()

我只需要打印 createCase 部分。

我将你的 xml 转储到变量 root 中,这里是你如何获得那块 XML:

import lxml.etree as ET
createCase=root.find('.//cre:createCase',namespaces=root.nsmap)
print ET.tostring(createCase, pretty_print=True)

打印这个:

<cre:createCase xmlns:cre="http://www.code.com/abc/V1/createCase" xmlns:wsu="http://docs.oasis-open.org/30.xsd" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
     <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: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>

编辑: OP 使用的是不带命名空间的旧版本 python/lxml,正确的代码是:

createCase=etree.tostring(root.find('.//{http://www.code.com/abc/V1/createCase}createCase'))
print etree.tostring(createCase, pretty_print=True)