我正在尝试使用XML的restful接口。我已将XML库的选择范围缩小到两个。
lxml.objectify似乎是我基于文档的首选,但我很挣扎。
Xmltodict似乎产生了更多的打字,可能不那么pythonic但我似乎能够取得更多的进展。
我有一个我用两者做的例子。 Xmltodict正在运行但是使用xml.objectify我似乎无法弄清楚如何访问/更改凭据的子元素。我的假设是我可以使用点符号来获取用户名和密码,但我没有成功。
首先,xmltodict代码:
from requests import post
import xmltodict
import pprint
pp=pprint.PrettyPrinter(indent=2)
req = '''<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Header>
<HTNGHeader xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://htng.org/1.1/Header/">
<From>
<Credential>
<password>PASSWORD</password>
<userName>UNAME</userName>
</Credential>
<systemId>2</systemId>
</From>
<To>
<systemId>2178</systemId>
</To>
</HTNGHeader>
</soap:Header>
<soap:Body />
</soap:Envelope>
'''
pwd = 'password'
uname = 'username'
url = 'https://url.com'
soap = xmltodict.parse(req)
soap['soap:Envelope']['soap:Header']['HTNGHeader']['From']['Credential']['password'] = pwd
soap['soap:Envelope']['soap:Header']['HTNGHeader']['From']['Credential']['userName'] = uname
soap_post = xmltodict.unparse(soap)
res = post(url, data=soap_post)
res_dict = xmltodict.parse(res.text)
pp.pprint(res_dict)
现在,lxml.objectify。
from requests import post
from lxml import etree, objectify
import pprint
pp=pprint.PrettyPrinter(indent=2)
req = '''<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Header>
<HTNGHeader xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://htng.org/1.1/Header/">
<From>
<Credential>
<password>PASSWORD</password>
<userName>UNAME</userName>
</Credential>
<systemId>2</systemId>
</From>
<To>
<systemId>2178</systemId>
</To>
</HTNGHeader>
</soap:Header>
<soap:Body />
</soap:Envelope>
'''
request_doc = req.encode('utf-8')
pwd = 'password'
uname = 'username'
url = 'https://url.com
soap = objectify.fromstring(request_doc)
#here is my problem
soap.Header.HTNGHeader.From.Credential.userName = uname
soap.Header.HTNGHeader.From.Credential.password = pwd
soap_post = etree.tostring(soap)
url = 'https://connect.channelrush.net/reservationreceive/pickup?providerId=2'
res = post(url, data=soap_post)
res_obj = objectify.fromstring(res.text.encode('utf-8'))
pp.pprint(res.text)
使用lxml.objectify,我得到以下异常:
File "f:\Dropbox\pms\channel_rush_interface_objectify.py", line 36, in <module>
soap.Header.HTNGHeader.From.Credential.userName = uname
File "c:\Python34\Lib\site-packages\lxml\objectify.pyd", line 230, in lxml.objectify.ObjectifiedElement.__getattr__ (src\lxml\objectify.c:4443)
File "c:\Python34\Lib\site-packages\lxml\objectify.pyd", line 451, in lxml.objectify._lookupChildOrRaise (src\lxml\objectify.c:7228)
builtins.AttributeError: no such child: {http://schemas.xmlsoap.org/soap/envelope/}HTNGHeader
有谁可以指出我如何让lxml.objectify工作?我应该坚持使用xmltodict吗?
答案 0 :(得分:0)
来自lxml.objectify documentation:
在标记查找期间,名称空间主要在幕后处理。如果在不指定命名空间的情况下访问Element的子级,则查找将使用父级的命名空间。 ...要访问与其父级不同的命名空间中的元素,可以使用getattr()... 为方便起见,还可以快速访问项目。 [示例已移除]
在您的情况下,请尝试:
soap.Header["{http://htng.org/1.1/Header/}HTNGHeader"].From.Credential.userName = uname
soap.Header["{http://htng.org/1.1/Header/}HTNGHeader"].From.Credential.password = pwd