繁体   English   中英

Python - 如何从输入 xml 中提取 soap 信封并作为 output 发送

[英]Python - how to extract a soap envelope from input xml and send as output

我是 python 的新手,我有一个输入 xml,它在子节点下嵌入了一个 soap 信封。 输入 xml:

    <SyncShipmentCreation  xmlns="http://schema.infor.com/InforOAGIS/2" releaseID="2">
        <ApplicationArea>       
           <CreationDateTime>2022-06-22T14:21:56Z</CreationDateTime>        
         </ApplicationArea>
        <DataArea>
           <Sync>
            <TenantID>TLD_TST</TenantID>            
           </Sync>
           <ShipmentCreation>
            <soapenv:Envelope xmlns:dns="http://schema.infor.com/InforOAGIS/2" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
                <soapenv:Header>                    
                </soapenv:Header>
                <soapenv:Body>
                    <ShipmentRequest xmlns="http://xxxyyyzzz.com/ShipmentMsgRequest">
                        ...
                    </ShipmentRequest>
                </soapenv:Body>
            </soapenv:Envelope>
        </ShipmentCreation>
    </DataArea>
</SyncShipmentCreation>        

soap 部分是需要的 Output。 喜欢

         <soapenv:Envelope xmlns:dns="http://schema.infor.com/InforOAGIS/2" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
                       ..............
            </soapenv:Envelope>

这可能吗? 我找不到如何解析/提取此值并将其分配给 output 变量。 请帮忙。

其中一种方法是按以下方式使用 lxml 库:

from lxml import etree

soap ="""[your xml above]"""

doc1 = etree.XML(soap)
#locate your target nodes and assign them to a second document
doc2 = doc1.xpath('//*[local-name()="ShipmentCreation"]/*')[0]
print(etree.tostring(doc2).decode())

output 应该是您预期的 output。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM