使用lxm打印Soap主体数据

2024-09-29 19:29:31 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个下面的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>

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

^{pr2}$

我只需要打印createCase部分。在


Tags: orghttpdocsopenenvelopeoasisxmlnswsse
1条回答
网友
1楼 · 发布于 2024-09-29 19:29:31

我将您的xml转储到变量root,下面是如何获取xml片段:

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

打印此:

^{pr2}$

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

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

相关问题 更多 >

    热门问题