my XML file is
JavaScript
x
17
17
1
<list>
2
<ProfileDefinition>
3
<string name="ID">nCGhwaZNpy6</string>
4
<string name="name">02.11.2013 Scott Mobile</string>
5
<decimal name="AccountID">10954</decimal>
6
<decimal name="TimeZoneID">-600</decimal>
7
</ProfileDefinition><ProfileDefinition>
8
<string name="ID">9JsG57bRUu6</string>
9
<string name="name">Huggies US-EN & CA-EN Test Town Responsive - Prod</string>
10
<decimal name="AccountID">10954</decimal>
11
<decimal name="TimeZoneID">-600</decimal>
12
</ProfileDefinition><ProfileDefinition>
13
<string name="ID">I3CJQ4gDkK6</string>
14
<string name="name">Huggies US-EN Brand Desktop - Prod</string>
15
<decimal name="AccountID">10954</decimal>
16
<decimal name="TimeZoneID">-600</decimal></ProfileDefinition>
17
my code is
JavaScript
1
32
32
1
import urllib2
2
3
theurl = 'https://ws.webtrends.com/v2/ReportService/profiles/?format=xml'
4
5
6
7
8
pagehandle = urllib2.urlopen(theurl)
9
10
11
12
##########################################################################
13
14
from xml.dom.minidom import parseString
15
16
file = pagehandle
17
18
19
data = file.read()
20
21
file.close()
22
23
dom = parseString(data)
24
25
xmlTag = dom.getElementsByTagName('string name="ID"')[0].toxml()
26
27
xmlData=xmlTag.replace('<string name="ID">','').replace('</string>','')
28
29
print xmlTag
30
31
print xmlData
32
I want to get value of element with tagname ‘string name=”ID”‘
but the error comes
JavaScript
1
5
1
Traceback (most recent call last):
2
File "C:UsersVaibhavDesktopWebtrendstest.py", line 43, in <module>
3
xmlTag = dom.getElementsByTagName('string name="ID"')[0].toxml()
4
IndexError: list index out of range
5
if i replace
JavaScript
1
2
1
dom.getElementsByTagName('string name="ID"')[0].toxml()
2
to
JavaScript
1
2
1
dom.getElementsByTagName('string')[0].toxml()
2
the output comes
“nCGhwaZNpy6”
since its the first element of that list but second element is
“02.11.2013 Scott Mobile”
which also get saved in list which i don’t want
however there are two string tag with name=”ID” and name=”name” how to access the string tag with name=”ID” only
Advertisement
Answer
string name="ID"
is not tag name. Only string
is tag name.
You have to compare name attribute value for each string
tag.
JavaScript
1
6
1
.
2
dom = parseString(data)
3
for s in dom.getElementsByTagName('string'):
4
if s.getAttribute('name') == 'ID':
5
print s.childNodes[0].data
6
I recommed you to use lxml or BeautifulSoup.
Following is equivalent code using lxml.
JavaScript
1
5
1
import lxml.html
2
dom = lxml.html.fromstring(data)
3
for s in dom.cssselect('string[name=ID]'):
4
print s.text
5