2010-08-10 49 views
4

我正在形成SOAPpy请求,但我无法弄清楚如何在标记中设置属性。这里是我的代码:如何使用python在SOAPpy中设置XML属性?

url = wsdlfile = 'https://stats2.overture.com/ExternalSOAP/statsPMCAPI_1_0.wsdl' 
n = 'urn:yahoo:overture:stats:3.0' 
server = WSDL.Proxy(wsdlfile) 
server.soapproxy.config.dumpSOAPOut = 1 
server.soapproxy.config.dumpSOAPIn = 1 
result = server.getAvailablePmcReports(ReportAuth = {'username': username, 'cookie': YBY}, ReportRequest= '') 
print(result) 

它输出这样的:

<?xml version="1.0" encoding="UTF-8"?> 
<SOAP-ENV:Envelope SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsi="http://www.w3.org/1999/XMLSchema-instance" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/1999/XMLSchema"> 
<SOAP-ENV:Body> 
<ns1:getAvailablePmcReports xmlns:ns1="urn:yahoo:overture:stats:3.0" SOAP-ENC:root="1"> 
<ReportRequest xsi:type="xsd:string"></ReportRequest> 
<ReportAuth> 
<username xsi:type="xsd:string">myuser</username> 
<cookie xsi:type="xsd:string">cookie here...</cookie> 
</ReportAuth> 
</ns1:getAvailablePmcReports> 
</SOAP-ENV:Body> 
</SOAP-ENV:Envelope> 

但我想是这样的:

<ReportRequest startRow="0" shownRows="200"> 

如何添加属性? 谢谢。

+0

你能,请提供完整的源XML文档和最终的XML文档你想从中形成?我可能会提供帮助。 – 2011-05-08 03:18:01

回答

3

您可以通过为ReportRequest关键字参数传递一个键入的值来完成此操作。例如,如果我的getAvailablePmcReports行改成这样:

from SOAPpy import Types 
result = server.getAvailablePmcReports(
    ReportAuth = {'username': username, 'cookie': YBY}, 
    ReportRequest= Types.stringType('', attrs={'startRow': 0, 'shownRows': 200})) 

产生的请求包括这样的标签:

<ReportRequest xsi:type="xsd:string" shownRows="200" startRow="0"></ReportRequest>