这段代码获取输入xml文件中每个配置文件中“children”的标记和文本。但是
小心
,这段代码不会试图编译出现在所有配置文件中的所有标记的完整集。如果某些配置文件具有其他配置文件没有的标记,那么这将导致编写csv文件的复杂化,因为在开始编写csv文件之前,您需要知道csv中有多少列。
def xml_event_info(eventID):
xml ='''
<?xml version="1.0" encoding="UTF-8"?>
<YourMembership>
<Version>2.25</Version>
<ApiKey>xxxx</ApiKey>
<CallID>001</CallID>
<></>
<SaPasscode>xxxx</SaPasscode>
<Call Method = "Sa.People.Profile.Get">
<ID>{}</ID>
</Call>
</YourMembership>
'''
headers = {'Content-Type': 'application/x-www-form-urlencoded'}
r = requests.post('https://api.yourmembership.com',
data=xml.format(eventID), headers=headers)
print(r.text)
xml ='''
<?xml version="1.0" encoding="utf-8" ?>
<YourMembership>
<Version>2.25</Version>
<ApiKey>xxxx</ApiKey>
<CallID>001</CallID>
<SaPasscode>xxxx</SaPasscode>
<Call Method="Sa.Groups.Group.GetMembershipLog">
<GroupID>12345</GroupID>
<ItemID></ItemID>
<StartDate></StartDate>
</Call>
</YourMembership>
'''
headers = {'Content-Type': 'application/x-www-form-urlencoded'}
r = requests.post('https://api.yourmembership.com', data=xml, headers=headers)
from xml.etree import et
tree = et.fromstring(r.text)
people_profiles = tree.findall('.//Sa.People.Profile.Get')
for people_profile in people_profiles:
'New profile'
for c, child in enumerate(people_profile.getchildren()):
print (child.tag, child.text)