Python 如何在suds中使用类型化解组器?
我有处理suds.client.client(…).service.GetFoo()的输出的现有代码。。现在,部分流程已经改变,我们不再使用SOAP,而是通过其他渠道接收相同的XML。我想通过使用suds类型的解组器重新使用现有代码,但到目前为止还没有成功 我使用基本的解组器完成了90%的步骤:Python 如何在suds中使用类型化解组器?,python,suds,Python,Suds,我有处理suds.client.client(…).service.GetFoo()的输出的现有代码。。现在,部分流程已经改变,我们不再使用SOAP,而是通过其他渠道接收相同的XML。我想通过使用suds类型的解组器重新使用现有代码,但到目前为止还没有成功 我使用基本的解组器完成了90%的步骤: tree = suds.umx.basic.Basic().process(xmlroot) 这给了我一个很好的带有属性的对象树,这样预先存在的代码就可以访问树[some_index].someAtt
tree = suds.umx.basic.Basic().process(xmlroot)
这给了我一个很好的带有属性的对象树,这样预先存在的代码就可以访问树[some_index].someAttribute
,但是值当然总是字符串,而不是整数、日期或其他什么,所以代码仍然不能按原样重复使用
原始类:
class SomeService(object):
def __init__(self):
self.soap_client = Client(some_wsdl_url)
def GetStuff(self):
return self.soap_client.service.GetStuff()
几乎起作用的替代品:
class SomeSourceUntyped(object):
def __init__(self):
self.url = some_url
def GetStuff(self):
xmlfile = urllib2.urlopen(self.url)
xmlroot = suds.sax.parser.Parser().parse(xmlfile)
if xmlroot:
# because the parser creates a document root above the document root
tree = suds.umx.basic.Basic().process(xmlroot)[0]
else:
tree = None
return tree
我理解suds.umx.typed.typed()的徒劳努力:
这是个晦涩难懂的问题
额外警告:我当然是在一个使用sud0.3.9的遗留系统中
编辑:代码的进一步发展,发现了如何创建
SchemaObject
s.我正在检查是否可以帮助我避免玩泡沫的半内部。不,不是这样,另一种方法是使用HTTP进行不相关的抓取,而不是通过抓取将XML提供给SOAP机制。我放弃了,改为使用该包,然后添加了一个适配器,使树的行为足够像一个SUDS树。
class SomeSourceTyped(object):
def __init__(self):
self.url = some_url
self.schema_file_name =
os.path.realpath(os.path.join(os.path.dirname(__file__),'schema.xsd'))
with open(self.schema_file_name) as f:
self.schema_node = suds.sax.parser.Parser().parse(f)
self.schema = suds.xsd.schema.Schema(self.schema_node, "", suds.options.Options())
self.schema_query = suds.xsd.query.ElementQuery(('http://example.com/namespace/','Stuff'))
self.xmltype = self.schema_query.execute(self.schema)
def GetStuff(self):
xmlfile = urllib2.urlopen(self.url)
xmlroot = suds.sax.parser.Parser().parse(xmlfile)
if xmlroot:
unmarshaller = suds.umx.typed.Typed(self.schema)
# I'm still running into an exception, so obviously something is missing:
# " Exception: (document, None, ), must be qref "
# Do I need to call the Parser differently?
tree = unmarshaller.process(xmlroot, self.xmltype)[0]
else:
tree = None
return tree