使用 python 在命名空间的帮助下创建 xml
Creating an xml with the help of namespaces using python
我必须创建一个包含名称空间的 SOAP 请求,文档应如下所示,
<soapenv:Envelope
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:v1="http://bhargavsaidama.com/services/schema/mser/mlistr/v1"
xmlns:v11="http://bhargavsaidama.com/services/schema/gs/rblock/v1"
xmlns:v12="http://bhargavsaidama.com/services/schemas/ut/mi/v1">
<soapenv:Header>
</soapenv:Header>
<soapenv:Body>
<v1:MLreq>
<v11:IDB>
</v11:IDB>
</v1:Mlreq>
<v1:Rparams>
<v12:MsgL>32</v12:MsgL>
</v1:Rparams>
</soapenv:Body>
</soapenv:Envelope>
但我知道使用 xml.etree.ElementTree 中的根和元素方法创建不带名称空间的 xml 文档而且我还知道通过使用 xpath 和 [= 解析来自 xml 文档的数据,该文档具有名称空间19=]lxml,但我无法理解如何创建上述文档。我试图找到教程,但在大多数地方都不太清楚。有人可以帮助我理解这一点吗?
谢谢
您可以为此使用 lxml 生成器。需要的样板文件有点重,但是嘿 XML。
from lxml import etree as etree
from lxml.builder import ElementMaker
soap_ns = ElementMaker(namespace='http://schemas.xmlsoap.org/soap/envelope/', nsmap={
'soapenv': 'http://schemas.xmlsoap.org/soap/envelope/',
'v1':'http://bhargavsaidama.com/services/schema/mser/mlistr/v1',
'v11': 'http://bhargavsaidama.com/services/schema/gs/rblock/v1',
'v12': 'http://bhargavsaidama.com/services/schemas/ut/mi/v1'
})
v1_ns = ElementMaker(namespace='http://bhargavsaidama.com/services/schema/mser/mlistr/v1')
v11_ns = ElementMaker(namespace='http://bhargavsaidama.com/services/schema/gs/rblock/v1')
v12_ns = ElementMaker(namespace='http://bhargavsaidama.com/services/schemas/ut/mi/v1')
root = soap_ns('Envelope')
body = soap_ns('Body')
mlreq = v1_ns('MLreq', v11_ns('IDB'))
rparams = v1_ns('Rparams', v12_ns('MsgL'))
body.append(mlreq)
body.append(rparams)
root.append(body)
结果:
print etree.tostring(root, pretty_print=True)
<soapenv:Envelope xmlns:v12="http://bhargavsaidama.com/services/schemas/ut/mi/v1" xmlns:v1="http://bhargavsaidama.com/services/schema/mser/mlistr/v1" xmlns:v11="http://bhargavsaidama.com/services/schema/gs/rblock/v1" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Body>
<v1:MLreq>
<v11:IDB/>
</v1:MLreq>
<v1:Rparams>
<v12:MsgL/>
</v1:Rparams>
</soapenv:Body>
</soapenv:Envelope>
我必须创建一个包含名称空间的 SOAP 请求,文档应如下所示,
<soapenv:Envelope
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:v1="http://bhargavsaidama.com/services/schema/mser/mlistr/v1"
xmlns:v11="http://bhargavsaidama.com/services/schema/gs/rblock/v1"
xmlns:v12="http://bhargavsaidama.com/services/schemas/ut/mi/v1">
<soapenv:Header>
</soapenv:Header>
<soapenv:Body>
<v1:MLreq>
<v11:IDB>
</v11:IDB>
</v1:Mlreq>
<v1:Rparams>
<v12:MsgL>32</v12:MsgL>
</v1:Rparams>
</soapenv:Body>
</soapenv:Envelope>
但我知道使用 xml.etree.ElementTree 中的根和元素方法创建不带名称空间的 xml 文档而且我还知道通过使用 xpath 和 [= 解析来自 xml 文档的数据,该文档具有名称空间19=]lxml,但我无法理解如何创建上述文档。我试图找到教程,但在大多数地方都不太清楚。有人可以帮助我理解这一点吗?
谢谢
您可以为此使用 lxml 生成器。需要的样板文件有点重,但是嘿 XML。
from lxml import etree as etree
from lxml.builder import ElementMaker
soap_ns = ElementMaker(namespace='http://schemas.xmlsoap.org/soap/envelope/', nsmap={
'soapenv': 'http://schemas.xmlsoap.org/soap/envelope/',
'v1':'http://bhargavsaidama.com/services/schema/mser/mlistr/v1',
'v11': 'http://bhargavsaidama.com/services/schema/gs/rblock/v1',
'v12': 'http://bhargavsaidama.com/services/schemas/ut/mi/v1'
})
v1_ns = ElementMaker(namespace='http://bhargavsaidama.com/services/schema/mser/mlistr/v1')
v11_ns = ElementMaker(namespace='http://bhargavsaidama.com/services/schema/gs/rblock/v1')
v12_ns = ElementMaker(namespace='http://bhargavsaidama.com/services/schemas/ut/mi/v1')
root = soap_ns('Envelope')
body = soap_ns('Body')
mlreq = v1_ns('MLreq', v11_ns('IDB'))
rparams = v1_ns('Rparams', v12_ns('MsgL'))
body.append(mlreq)
body.append(rparams)
root.append(body)
结果:
print etree.tostring(root, pretty_print=True)
<soapenv:Envelope xmlns:v12="http://bhargavsaidama.com/services/schemas/ut/mi/v1" xmlns:v1="http://bhargavsaidama.com/services/schema/mser/mlistr/v1" xmlns:v11="http://bhargavsaidama.com/services/schema/gs/rblock/v1" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Body>
<v1:MLreq>
<v11:IDB/>
</v1:MLreq>
<v1:Rparams>
<v12:MsgL/>
</v1:Rparams>
</soapenv:Body>
</soapenv:Envelope>