Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/cmake/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在xml中创建具有命名空间的子元素_Python_Xml_Soap_Namespaces - Fatal编程技术网

Python 在xml中创建具有命名空间的子元素

Python 在xml中创建具有命名空间的子元素,python,xml,soap,namespaces,Python,Xml,Soap,Namespaces,我想创建这个xml,但我不知道如何使用名称空间创建子元素IsAddSegments: <?xml version="1.0" encoding="UTF-8"?> <soapenv:Envelope xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope"> <soapenv:Body> <ISAddSegments xmlns="http://www.blue-order.c

我想创建这个xml,但我不知道如何使用名称空间创建子元素
IsAddSegments

<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope">
    <soapenv:Body>
        <ISAddSegments xmlns="http://www.blue-order.com/ma/integrationservicews/api">
            <accessKey>key</accessKey>
            <objectID>
                <guid>guid</guid>
            </objectID>
            <StratumName>STRATUM</StratumName>
            <Segments>
                <Segment>
                    <Begin>00:00:00:00</Begin>
                    <Content>TEXT</Content>
                    <End>00:00:10:00</End>
                </Segment>
            </Segments>
        </ISAddSegments>
    </soapenv:Body>
</soapenv:Envelope>
但是这在主元素中创建了一个额外的名称空间,这不是我需要的

<?xml version='1.0' encoding='utf-8'?>
<soapenv:Envelope xmlns:ns1="http://www.blue-order.com/ma/integrationservicews/api" xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope">
    <soapenv:Body>
        <ns1:ISAddSegments>

考虑使用专用的SOAP模块,如
肥皂水
。然后,您可以通过向
元素提供
ns
来创建自定义名称空间。该值应该是一个元组,包含名称空间名称和定义该名称的url:

from suds.sax.element import Element

custom_namespace = ('custom_namespace', 'http://url/namespace.xsd')
element_with_custom_namespace = Element('Element', ns=custom_namespace)

print(element_with_custom_namespace)
# <custom_namespace:Element xmlns:custom_namespace="http://url/namespace.xsd"/>
来自suds.sax.element导入元素
自定义_名称空间=('custom_名称空间','http://url/namespace.xsd')
元素,带有自定义名称空间=元素('element',ns=自定义名称空间)
打印(带有自定义名称空间的元素)
# 

我用
lxml解决了这个问题:

from lxml import etree as etree

ns1 = 'http://www.w3.org/2003/05/soap-envelope'
ns2 = 'http://www.blue-order.com/ma/integrationservicews/api'

Envelope = etree.Element('{ns1}Envelope', nsmap = {'soapenv': ns1})
Body = etree.SubElement(Envelope, '{ns1}Body')
ISAddSegments = etree.SubElement(Body, 'ISAddSegments', nsmap = {None : ns2})
accessKey = etree.SubElement(ISAddSegments, 'accessKey')
...

名称空间定义应该位于xml文档的顶部,因此根据该定义,lxml正在生成正确的xml?我找不到很多关于
suds
from lxml import etree as etree

ns1 = 'http://www.w3.org/2003/05/soap-envelope'
ns2 = 'http://www.blue-order.com/ma/integrationservicews/api'

Envelope = etree.Element('{ns1}Envelope', nsmap = {'soapenv': ns1})
Body = etree.SubElement(Envelope, '{ns1}Body')
ISAddSegments = etree.SubElement(Body, 'ISAddSegments', nsmap = {None : ns2})
accessKey = etree.SubElement(ISAddSegments, 'accessKey')
...