在将其作为请求发送之前重写 SOAP xml

问题描述 投票:0回答:1

我需要在发出请求之前重写 SOAP XML 文件。 我可以使用静态文件发送请求并接收响应,没有任何问题,但我需要更改一些值 我尝试使用 ElementTree 来操作文件,但找不到标签和值。

该文件具有以下格式:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
   <soapenv:Header/>
   <soapenv:Body>
      <bbs:QueryDRRequestMsg>
         <RequestHeader>
                    ...
                </RequestHeader>
         <QueryDRRequest>
            <!--Optional:-->
             <bbs:SubAccessCode>
               <bbc:PrimaryIdentity>56795810005</bbc:PrimaryIdentity>
            </bbs:SubAccessCode>
            <bbs:TimePeriod>
               <bbs:StartTime>20150901010000</bbs:StartTime>
               <bbs:EndTime>20150930010000</bbs:EndTime>
            </bbs:TimePeriod>
            <bbs:TotalDRNum>2</bbs:TotalCDRNum>
            <bbs:BeginRowNum>0</bbs:BeginRowNum>
            <bbs:FetchRowNum>10</bbs:FetchRowNum>
         </QueryDRRequest>
      </bbs:QueryDRRequestMsg>
   </soapenv:Body>
</soapenv:Envelope>

我需要更改标签的值

bbc:主要身份

bbs:开始时间

这些值将从 html 表单中获取,并在按请求发送文件之前使用该数据重写文件。

提前致谢

python xml soap elementtree
1个回答
1
投票

尝试使用 ElementTree.iter 查看标签的完整文本,如下所示

import xml.etree.cElementTree as ET

tree = ET.parse("filexml")
root = tree.getroot()

for child_root in root.iter():
    print  child_root.tag, child_root.attrib

这样你就会看到xml文件元素的完整标签。

要修改你的文件,你可以尝试这个

import xml.etree.cElementTree as ET

tree = ET.parse("file.xml")
root = tree.getroot()

for child_root in root.iter('{http://some_tag}PrimaryIdentity'):
    new_identity = "111111111"
    child_root.text = new_identity
    child_root.set('update','yes')

tree.write('output.xml')
© www.soinside.com 2019 - 2024. All rights reserved.