我的 XML 文件是
<list>
<ProfileDefinition>
<string name="ID">nCGhwaZNpy6</string>
<string name="name">02.11.2013 Scott Mobile</string>
<decimal name="AccountID">10954</decimal>
<decimal name="TimeZoneID">-600</decimal>
</ProfileDefinition><ProfileDefinition>
<string name="ID">9JsG57bRUu6</string>
<string name="name">Huggies US-EN & CA-EN Test Town Responsive - Prod</string>
<decimal name="AccountID">10954</decimal>
<decimal name="TimeZoneID">-600</decimal>
</ProfileDefinition><ProfileDefinition>
<string name="ID">I3CJQ4gDkK6</string>
<string name="name">Huggies US-EN Brand Desktop - Prod</string>
<decimal name="AccountID">10954</decimal>
<decimal name="TimeZoneID">-600</decimal></ProfileDefinition>
我的代码是
import urllib2
theurl = 'https://ws.webtrends.com/v2/ReportService/profiles/?format=xml'
pagehandle = urllib2.urlopen(theurl)
##########################################################################
from xml.dom.minidom import parseString
file = pagehandle
data = file.read()
file.close()
dom = parseString(data)
xmlTag = dom.getElementsByTagName('string name="ID"')[0].toxml()
xmlData=xmlTag.replace('<string name="ID">','').replace('</string>','')
print xmlTag
print xmlData
我想用标签名'string name="ID"'获取元素的值
但错误来了
Traceback (most recent call last):
File "C:\Users\Vaibhav\Desktop\Webtrends\test.py", line 43, in <module>
xmlTag = dom.getElementsByTagName('string name="ID"')[0].toxml()
IndexError: list index out of range
如果我更换
dom.getElementsByTagName('string name="ID"')[0].toxml()
至
dom.getElementsByTagName('string')[0].toxml()
输出来了
“nCGhwaZNpy6”
因为它是该列表的第一个元素,但第二个元素是
“02.11.2013 斯科特移动”
这也保存在我不想要的列表中
但是有两个带有 name="ID" 和 name="name" 的字符串标签 如何仅访问带有 name="ID" 的字符串标签