Python 如何将suds对象转换为xml字符串
这是对这个问题的重复:Python 如何将suds对象转换为xml字符串,python,xml,soap,suds,Python,Xml,Soap,Suds,这是对这个问题的重复: 但问题尚未得到回答:“totxt”不是客户机类上的属性。 不幸的是,我没有声誉来添加评论追加,'w'=>创建+写入) 使用带有语句的(文件上下文管理器) 不需要返回响应字符串:使用异常管理器。要捕获的异常可以是 分析 以下电话: customer = c.factory.create("ns0:CustomerType") 动态构造一个CustomerType,并返回一个CustomerType实例customer 我认为您可以反思您的客户对象,尝试以下方法: vars
但问题尚未得到回答:“totxt”不是客户机类上的属性。 不幸的是,我没有声誉来添加评论<所以我再次问: 有没有办法将suds对象转换为其xml 我这样问是因为我已经有了一个使用wsdl文件并向Web服务发送数据的系统。但是现在客户希望将XML存储为文件(以便稍后手动导入)。因此,我只需要两种方法来写入数据:一种写入Web服务(已实现并已测试),另一种(尚未实现)写入文件。 要是我能做这样的东西就好了:
xml\u as\u string=My\u suds\u object.to\u xml()
下面的代码只是一个示例,不运行。而且也不优雅。没关系。我希望你了解我想要实现的目标:
我有一个功能“write_customer_obj_webservice”,可以正常工作。现在我想编写函数“write_customer_obj_xml_file”。您在
写入客户对象xml\u文件
功能中遇到一些问题:
修复错误路径:
output_filename = r'C:\temp\test.xml'
下面这一行就是问题所在。“to_xml”不存在,我找不到一种方法来实现它
什么类型的客户<代码>类型(客户)
为什么选择a模式?('a'=>追加,'w'=>创建+写入)
使用带有语句的(文件上下文管理器)
不需要返回响应字符串:使用异常管理器。要捕获的异常可以是
分析
以下电话:
customer = c.factory.create("ns0:CustomerType")
动态构造一个CustomerType
,并返回一个CustomerType
实例customer
我认为您可以反思您的客户
对象,尝试以下方法:
vars(customer) # display the object attributes
help(customer) # display an extensive help about your instance
另一种方法是手动尝试WSDL URL,并查看XML结果。
您可以获得CustomerType
对象的完整描述
然后?
然后,通过属性列表,您可以创建自己的XML。使用XML模板并用对象属性填充它
您还可以找到神奇的函数(to_xml
),它可以为您完成这项工作。但是,我不确定XML格式是否符合您的需要。我找到了一种适合我的方法。诀窍是使用选项“nosend=True”创建客户机。
在地图上写着:
nosend-创建soap信封,但不发送。指定后,方法调用将返回RequestContext,而不是发送它
RequestContext对象具有属性信封。这是XML字符串
一些伪代码来说明:
c = suds.client.Client(url, nosend=True)
customer = c.factory.create("ns0:CustomerType")
customer.name = "Doe J."
customer.age = 42
response = c.service.save(someparameters, None, None, customer)
print response.envelope # This prints the XML string that would have been sent.
你有代码样本吗?添加了代码样本。我知道。这只是用来说明问题的伪代码。路径不是问题所在,模式“a”或带有语法或错误处理的模式也不是问题所在。客户类型为suds.sudsobject.customerType我改进了我的回答,以解释为什么很难做出简单的回答,并为您提供我的分析…
customer = c.factory.create("ns0:CustomerType")
vars(customer) # display the object attributes
help(customer) # display an extensive help about your instance
c = suds.client.Client(url, nosend=True)
customer = c.factory.create("ns0:CustomerType")
customer.name = "Doe J."
customer.age = 42
response = c.service.save(someparameters, None, None, customer)
print response.envelope # This prints the XML string that would have been sent.
client = Client(url)
client.factory.create('somename')
# The last XML request by client
client.last_sent()
# The last XML response from Web Service
client.last_received()