我需要從這個XML腳本中獲取用戶名。我閱讀了Python中的元素樹庫,並嘗試了他們對我的XML文件所做的工作,但無濟於事。我所需要的只是一個獲取用戶名的例子,這會非常有幫助,並幫助我理解其餘部分。首先是XML文件,然後是代碼。從人性化的顯示如何從XML中獲取特定信息(Python)
<?xml version="1.0" encoding="UTF-8" ?>
- <definitions name="GetEmailCert" targetNamespace="urn:GetEmailCert"
xmlns="http://schemas.xmlsoap.org/wsdl/"
xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
xmlns:tns="urn:GetEmailCert" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
- <message name="GetEmailCertRequest">
<part name="username" type="xsd:string" />
</message>
- <message name="GetEmailCertResponse">
<part name="fullname" type="xsd:string" />
<part name="email" type="xsd:string" />
<part name="certificate" type="xsd:string" />
</message>
from xml.etree.ElementTree import ElementTree
tree = ElementTree()
tree.parse("test.xml")
root = tree.getroot()
root.tag
root.attrib
for child in root:
print child.tag, child.attrib
#This one works
for username in root.iter('username'):
print username
#This one I do not know how to correctly implement
的'-'在你的XML字符使其無效。它們是否存在於實際文件中? –