这与此问题重复:
How to convert suds object to xml
但问题没有得到回答:" totxt"不是Client类的属性。
不幸的是,我缺乏添加评论的声誉。
所以我再问一遍:
有没有办法将suds对象转换为xml?
我问这个是因为我已经有一个系统使用wsdl文件并将数据发送到web服务。但现在客户希望将XML作为文件存储(稍后手动导入)。所以我需要的是两种写入数据的方法:一种写入Web服务(实现和测试),另一种(尚未实现)写入文件。
如果我能做出这样的事情:
xml_as_string = My_suds_object.to_xml()
以下代码只是一个示例,不会运行。而且它并不优雅。没关系。我希望你明白我想要实现的目标:
我有函数" write_customer_obj_webservice"这样可行。现在我想编写函数" write_customer_obj_xml_file"。
import suds
def get_customer_obj():
wsdl_url = r'file:C:/somepathhere/Customer.wsdl'
service_url = r'http://someiphere/Customer'
c = suds.client.Client(wsdl_url, location=service_url)
customer = c.factory.create("ns0:CustomerType")
return customer
def write_customer_obj_webservice(customer):
wsdl_url = r'file:C:/somepathhere/Customer.wsdl'
service_url = r'http://someiphere/Customer'
c = suds.client.Client(wsdl_url, location=service_url)
response = c.service.save(someparameters, None, None, customer)
return response
def write_customer_obj_xml_file(customer):
output_filename = r'C\temp\testxml'
# The following line is the problem. "to_xml" does not exist and I can't find a way to do it.
xml = customer.to_xml()
fo = open(output_filename, 'a')
try:
fo.write(xml)
except:
raise
else:
response = 'All ok'
finally:
fo.close()
return response
# Get the customer object always from the wsdl.
customer = get_customer_obj()
# Since customer is an object, setting it's attributes is very easy. There are very complex objects in this system.
customer.name = "Doe J."
customer.age = 42
# Write the new customer to a webservice or store it in a file for later proccessing
if later_processing:
response = write_customer_obj_xml_file(customer)
else:
response = write_customer_obj_webservice(customer)
答案 0 :(得分:1)
我发现了一种适合我的方式。诀窍是使用选项“nosend = True”创建客户端。
在documentation中它说:
nosend - 创建肥皂信封但不发送。指定时,方法调用返回RequestContext而不是发送它。
RequestContext对象具有属性envelope。这是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.
答案 1 :(得分:-1)
write_customer_obj_xml_file
函数中存在一些问题:
修复错误路径:
output_filename = r'C:\temp\test.xml'
以下行是问题所在。 " to_xml"不存在,我找不到办法。
客户的类型是什么? type(customer)
?
xml = customer.to_xml() # to be continued...
为什么模式=' a'? (' a' =>追加,' w' =>创建+写入)
使用with
语句(文件上下文管理器)。
with open(output_filename, 'w') as fo:
fo.write(xml)
不需要返回响应字符串:使用异常管理器。捕获的例外可以是EnvironmentError。
以下电话:
customer = c.factory.create("ns0:CustomerType")
动态构建CustomerType
,并返回CustomerType
个实例customer
。
我认为您可以反省您的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格式是否符合您的需要。
答案 2 :(得分:-1)
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()