Python 如何通过在SOAP上替换XML中的值来发送XML请求?
我的模板目录中有一个XML文件,这个XML文件有几个占位符。我需要用数据库中的值和其他值的计算(基于用户输入)填充这些占位符,然后将其发送到URL,URL将处理此XML并返回XML响应 以下是我的XML文件的外观: my_file.xmlPython 如何通过在SOAP上替换XML中的值来发送XML请求?,python,xml,django,soap,python-requests,Python,Xml,Django,Soap,Python Requests,我的模板目录中有一个XML文件,这个XML文件有几个占位符。我需要用数据库中的值和其他值的计算(基于用户输入)填充这些占位符,然后将其发送到URL,URL将处理此XML并返回XML响应 以下是我的XML文件的外观: my_file.xml <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:web="http://some_url.api.com" xmlns:web1="htt
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:web="http://some_url.api.com" xmlns:web1="http://another_url.api.com">
<soapenv:Header>
<web:AuthenticationToken>
<web:licenseKey> string </web:licenseKey>
<web:password> string </web:password>
<web:username> string </web:username>
</web:AuthenticationToken>
</soapenv:Header>
<soapenv:Body>
<web:Shipping>
<web:ShipRequest>
<web1:dtnCtry> string </web1:dtnCtry>
<web1:dtnZC> string </web1:dtnZC>
<web1:details>
<!--Zero or more repetitions:-->
<web1:ShipRequestDetail>
<web1:class> string </web1:class>
<web1:wt> string </web1:wt>
</web1:ShipRequestDetail>
</web1:details>
<web1:orgCtry> string </web1:orgCtry>
<web1:orgZC> string </web1:orgZC>
<web1:shipDateCCYYMMDD> string </web1:shipDateCCYYMMDD>
<web1:shipID> string </web1:shipID>
<web1:tarName> string </web1:tarName>
</web:ShipRequest>
</web:Shipping>
</soapenv:Body>
一串
一串
一串
一串
一串
一串
一串
一串
一串
一串
一串
一串
这些字符串值中的每一个都必须替换为实际的数据库值。我如何进行这个过程
我已经检查了多个论坛、网站和SO内部的多个问题,每一个都推荐了不同的方法。作为一个新手,我很难选择一个,这是非常压倒性的
这里有人能解释一下从头开始的过程吗?谢谢 考虑使用Python的lxml
将参数传递到XSLT脚本中:
XSLT(另存为.xsl文件)
输出
<?xml version="1.0" encoding="utf-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:web="http://some_url.api.com" xmlns:web1="http://another_url.api.com">
<soapenv:Header>
<web:AuthenticationToken>
<web:licenseKey>123123</web:licenseKey>
<web:password>password</web:password>
<web:username>Di437</web:username>
</web:AuthenticationToken>
</soapenv:Header>
<soapenv:Body>
<web:Shipping>
<web:ShipRequest>
<web1:dtnCtry>Python Country</web1:dtnCtry>
<web1:dtnZC>Django ZC</web1:dtnZC>
<web1:details>
<!-- Zero or more repetitions: -->
<web1:ShipRequestDetail>
<web1:class>my class</web1:class>
<web1:wt>my wt</web1:wt>
</web1:ShipRequestDetail>
</web1:details>
<web1:orgCtry>XML Country</web1:orgCtry>
<web1:orgZC>Soap ZC</web1:orgZC>
<web1:shipDateCCYYMMDD>2018-06-17 12:00</web1:shipDateCCYYMMDD>
<web1:shipID>888999</web1:shipID>
<web1:tarName>stackoverflow</web1:tarName>
</web:ShipRequest>
</web:Shipping>
</soapenv:Body>
</soapenv:Envelope>
123123
密码
Di437
蟒蛇国家
Django ZC
我的班级
我的体重
XML国家
肥皂ZC
2018-06-17 12:00
888999
栈溢出
看看lxml。您可能还想签出XSL来解析XML
import lxml.etree as et
# LOAD XML AND XSL
doc = et.parse('my_file.xml')
xsl = et.parse('my_script.xsl')
# CONFIGURE TRANSFORMER
transform = et.XSLT(xsl)
# RUN TRANSFORMATION WITH PARAMS
result = transform(doc,
licenseKey_param = et.XSLT.strparam('123123'),
password_param = et.XSLT.strparam('password'),
username_param = et.XSLT.strparam('Di437'),
dtnCtry_param = et.XSLT.strparam('Python Country'),
dtnZC_param = et.XSLT.strparam('Django ZC'),
class_param = et.XSLT.strparam('my class'),
wt_param = et.XSLT.strparam('my wt'),
orgCtry_param = et.XSLT.strparam('XML Country'),
orgZC_param = et.XSLT.strparam('Soap ZC'),
shipDateCCYYMMDD_param = et.XSLT.strparam('2018-06-17 12:00'),
shipID_param = et.XSLT.strparam('888999'),
tarName_param = et.XSLT.strparam('stackoverflow'))
# PRINT RESULT
print(result)
# SAVE TO FILE
with open('output.xml', 'wb') as f:
f.write(result)
<?xml version="1.0" encoding="utf-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:web="http://some_url.api.com" xmlns:web1="http://another_url.api.com">
<soapenv:Header>
<web:AuthenticationToken>
<web:licenseKey>123123</web:licenseKey>
<web:password>password</web:password>
<web:username>Di437</web:username>
</web:AuthenticationToken>
</soapenv:Header>
<soapenv:Body>
<web:Shipping>
<web:ShipRequest>
<web1:dtnCtry>Python Country</web1:dtnCtry>
<web1:dtnZC>Django ZC</web1:dtnZC>
<web1:details>
<!-- Zero or more repetitions: -->
<web1:ShipRequestDetail>
<web1:class>my class</web1:class>
<web1:wt>my wt</web1:wt>
</web1:ShipRequestDetail>
</web1:details>
<web1:orgCtry>XML Country</web1:orgCtry>
<web1:orgZC>Soap ZC</web1:orgZC>
<web1:shipDateCCYYMMDD>2018-06-17 12:00</web1:shipDateCCYYMMDD>
<web1:shipID>888999</web1:shipID>
<web1:tarName>stackoverflow</web1:tarName>
</web:ShipRequest>
</web:Shipping>
</soapenv:Body>
</soapenv:Envelope>