程序问答   发布时间:2022-06-02  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了使用 python 和 xml.etree.ElementTree 解析 XML大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决使用 python 和 xml.etree.ElementTree 解析 XML?

开发过程中遇到使用 python 和 xml.etree.ElementTree 解析 XML的问题如何解决?下面主要结合日常开发的经验,给出你关于使用 python 和 xml.etree.ElementTree 解析 XML的解决方法建议,希望对你解决使用 python 和 xml.etree.ElementTree 解析 XML有所启发或帮助;

我正在尝试从 BambooHR API 中获取 xml 数据,然后在我们公司的 Google 帐户中@R_508_10589@。现在我正在努力通过 xml。我见过的每个例子都有不同标签名称的数据,其中我的相同('字段)但附加了一个 ID

这是我的 xml 响应

@H_301_4@<?xml version="1.0"?> <directory> <fIEldset> <fIEld ID="displayname">display name</fIEld> <fIEld ID="firstname">First name</fIEld> <fIEld ID="lastname">last name</fIEld> <fIEld ID="preferredname">Preferred name</fIEld> <fIEld ID="jobtitle">Job title</fIEld> <fIEld ID="mobilePhone">Mobile Phone</fIEld> <fIEld ID="workEmail">Work Email</fIEld> <fIEld ID="department">Department</fIEld> <fIEld ID="LOCATIOn">LOCATIOn</fIEld> <fIEld ID="division">division</fIEld> <fIEld ID="linkedIn">linkedIn URL</fIEld> <fIEld ID="supervisor">Manager</fIEld> <fIEld ID="photoUploaded">employee photo</fIEld> <fIEld ID="photoUrl">Photo URL</fIEld> <fIEld ID="canUploadPhoto">Can Upload Photo</fIEld> </fIEldset> <employees> <employee ID="379"> <fIEld ID="displayname">test one</fIEld> <fIEld ID="firstname">test</fIEld> <fIEld ID="lastname">one</fIEld> <fIEld ID="preferredname"></fIEld> <fIEld ID="jobtitle">Assistant</fIEld> <fIEld ID="mobilePhone">123456789</fIEld> <fIEld ID="workEmail">test.one@email.com</fIEld> <fIEld ID="department">Recruitment</fIEld> <fIEld ID="LOCATIOn">Remote</fIEld> <fIEld ID="division">company name</fIEld> <fIEld ID="linkedIn"></fIEld> <fIEld ID="supervisor">test supervisor</fIEld> <fIEld ID="photoUploaded">true</fIEld> <fIEld ID="photoUrl">"https://image.com"</fIEld> <fIEld ID="canUploadPhoto">yes</fIEld> </employee> <employee ID="398"> <fIEld ID="displayname">tester two</fIEld> <fIEld ID="firstname">tester</fIEld> <fIEld ID="lastname">two</fIEld> <fIEld ID="preferredname"></fIEld> <fIEld ID="jobtitle">Recruitment</fIEld> <fIEld ID="mobilePhone">987654321</fIEld> <fIEld ID="workEmail">tester.two@company.com</fIEld> <fIEld ID="department">Recruitment</fIEld> <fIEld ID="LOCATIOn">Remote</fIEld> <fIEld ID="division">company</fIEld> <fIEld ID="linkedIn"></fIEld> <fIEld ID="supervisor">test supervisor</fIEld> <fIEld ID="photoUploaded">true</fIEld> <fIEld ID="photoUrl">"https://image.com"</fIEld> <fIEld ID="canUploadPhoto">yes</fIEld> </employee> </employees> </directory>

这是我一直在研究的一些代码。我从 API 中提取它后,将我创建并保存在本地的 xml 文件传递​​给该函数

@H_301_4@def parse_XML(xml_filE): tree = ET.parse(xml_filE) root = tree.getroot() print('prinTing root....') print(root.tag,root.attrib) #directory {} for emp in root.iter('employee'): for employee in emp: #if employee work email == '': # get all users data ready to send to Google to create a new account

所以我试图查看用户的工作电子邮件是否为 == "",这意味着他们没有谷歌帐户,然后我会将用户信息发送给谷歌以创建帐户

我遇到的问题是因为标签都是一样的 我在获取标签的值时遇到了麻烦

如果您能帮助将所有用户编入员工列表或推荐实现此目的的最佳方法,那就太好了。或者提出问题,我可以尝试具体说明

解决方法

for emp in root.iter('employee') 已经遍历了所有 <employee> 节点。您剩下要做的就是迭代所有 <field> 节点并检查 workEmail 字段的内容是否为空:

for emp in root.iter('employee'):
    for field in emp.iter('field'):
        if field.attrib['id'] == 'workEmail' and field.text == '':
            print('this employee has no email')

更新:由于您对几个特定的​​ <field> 节点感兴趣,因此使用 xpath 来获取您需要的节点而不是内部循环可能会有所帮助:

for emp in root.iter('employee'):
    email = emp.find('field[@id="workEmail"]').text
    if email == '':
        first_name = emp.find('field[@id="firstName"]').text
        last_name = emp.find('field[@id="lastName"]').text
        # do whatever with these details

大佬总结

以上是大佬教程为你收集整理的使用 python 和 xml.etree.ElementTree 解析 XML全部内容,希望文章能够帮你解决使用 python 和 xml.etree.ElementTree 解析 XML所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。