我试图从一个长的 xml 文档中获取一些属性。具体来说,我希望获得该代码可靠地完成的阶段 cfs 和 ft 级别。困难是我似乎无法弄清楚如何从标签中提取时间戳作为日期时间值,类似于:
<ns1:value qualifiers="P" dateTime="2012-11-01T18:45:00.000-05:00">54800</ns1:value>
非常感谢任何帮助和改进建议。
def getLevels(gaugeId):
# create url string 00060=cfs and 00065=ft
urlRoot = "http://waterservices.usgs.gov/nwis/iv/?format=waterml,1.1&sites="
urlTail = "¶meterCd=00060,00065"
url = urlRoot + str(gaugeId) + urlTail
del urlRoot, urlTail
# open connection to url
urlFile = urllib2.urlopen(url)
# convert urlFile to string data:
urlData = urlFile.read()
# close file to release memory
urlFile.close()
# parse downloaded xml
domData = parseString(urlData)
# extract xml element values for stage cfs and ft
index = 0
elementCount = domData.getElementsByTagName("ns1:value").length
output = []
while elementCount >= index:
xmlString = domData.getElementsByTagName("ns1:value")[index].toxml()
output.append(stripXmlTags(xmlString))
index = index + 1
# extract and return
return output