使用以下XML數據我想獲取我通過Python代碼調用的相關鍵的值。我想在不使用任何第三方庫的情況下完成此任務。在SelectSingleNode上使用Python解析XML
<Userinfo>
<UserData>
<item key="DateOfBirth" value="19851103" />
<item key="FirstName" value="John" />
<item key="LastName" value="Dicaprio" />
<item key="Gender" value="M" />
<item key="Email" value="[email protected]" />
<item key="ContactNo" value="235625341" />
</UserData>
</Userinfo>
從上面的xml代碼中,我想從我在下面的python代碼中調用的鍵中提取值。
def ExtractXml(args):
url = '....'
wc = System.Net.WebClient()
xml = wc.DownloadString(url)
doc = System.Xml.XmlDocument()
doc.LoadXml(xml)
root = doc.DocumentElement
nsmgr = System.Xml.XmlNamespaceManager(doc.NameTable)
#nsmgr.AddNamespace('ns','http://schemas.microsoft.com/developer/msbuild/2003')
node = root.SelectNodes('/Userinfo/UserData',nsmgr)
tcount=root.SelectNodes('/Userinfo/UserData').Count
if not node:
ServiceDesk.Log.PrintError('No condition node')
return
r=[]
t={}
counts=0
for itemNode in node:
counts += 1
fullname = xstr(itemNode.SelectSingleNode("/item[@key='FirstName']/@value",nsmgr))
empname = xstr(itemNode.SelectSingleNode("/item[@key='LastName']/@value",nsmgr))
cardcountry = xstr(itemNode.SelectSingleNode("/item[@key='Email']/@value",nsmgr))
#birthdate = ServiceDesk.Common.ParseDateTime(itemNode.SelectSingleNode("item[@key='DateOfBirth']"))
t = {'counter':counts,'FirstName':fullname,'LastName':empname,'Email':cardcountry,'__rowid__':counter,'__totalcount__':tcount}
r.append(t)
return r
使用以下代碼,它不會檢索SelectSingleNode調用上相關鍵的值。提前致謝。
ElementTree庫不適用本..:/ – Zoom1992
在發佈之前,我確實檢查了ElementTree是否包含在IronPython中(它似乎是https://ironpython-test.readthedocs.io/en/latest/library/xml.etree.elementtree.html)。爲什麼它不在這裏工作? – Tomatohater
我收到這個錯誤,'LightException'對象沒有屬性'etree' – Zoom1992