现在有a.xml

<CallLogs>
<CallLog>
<Flags>2</Flags>
<Duration>20</Duration>
<StartTime>2019-05-10 14:46:55</StartTime>
<ContactName></ContactName>
<PhoneNumber>13869833123</PhoneNumber>
</CallLog>
<CallLog>
<Flags>2</Flags>
<Duration>20</Duration>
<StartTime>2019-05-10 14:46:55</StartTime>
<ContactName></ContactName>
<PhoneNumber>13869833456</PhoneNumber>
</CallLog>
</CallLogs>

需要导出所有手机号

#引入parse的包
from xml.dom.minidom import parse
if __name__ == '__main__':
doc = parse("a.xml") # 先把xml文件加载进来
root = doc.documentElement # 获取元素的根节点
books = root.getElementsByTagName('CallLog') # 找到子节点,得到的是一个数组
output=""
for x in books:
output+=x.getElementsByTagName("PhoneNumber")[0].childNodes[0].data+"\n"
with open("out.txt",'w', encoding='utf-8')as f:
f.write(output)
f.close()

xml 如何获取到标签的内容呢

路径 


path=r'H:\project\android\demo\browser\app\src\main\res\values\strings.xml'


内容

<?xml version="1.0" encoding="utf-8"?>

<resources>

<!-- App -->
<string name="app_name">AnGuo Browser</string>
<resources>

解析

doc = parse(path)  # 先把xml文件加载进来
root = doc.documentElement # 获取元素的根节点
sub_element_obj = root.getElementsByTagName('string') # 找到子节点,得到的是一个数组
output = ""
for i in range(len(sub_element_obj)):
stringTag=sub_element_obj[i].childNodes[0].data
stringContent=sub_element_obj[i].getAttribute("name")
print(stringContent)
print(stringTag)

结果

app_name
AnGuo Browser