How do I get the content between two xml tags in python? -


import xml.dom.minidom  water = """ <channel> <item> <title>water</title> <link>http://www.water.com</link> </item> <item> <title>fire</title> <link>http://www.fire.com</link> </item> </channel>"""  dom=xml.dom.minidom.parsestring(water) linklist = dom.getelementsbytagname('link') print (len(linklist)) 

using minidom, want content between link , /link string. please let me know how to.

if want stick xml.dom.minidom call .firstchild.nodevalue. example, stored links in variable "linklist", print them iterate through them , call .firstchild.nodevalue, this...

for link in linklist:     print link.firstchild.nodevalue 

prints...

http://www.water.com http://www.fire.com 

more detailed answer here.... get element value minidom python


in response other question:
if wanted specific element need know either in document or search it.

for example, if knew link wanted second link in xml document do...

# variable fire_link dom element of second link in xml file fire_link = linklist[1] 

however, if wanted link not know in document, have search it. here example...

# fire_link list each element dom element containing http://www.fire.com link fire_links = [l l in linklist if l.firstchild.nodevalue == 'http://www.fire.com']  # take first element fire_link = fire_links[0] 

Comments

Popular posts from this blog

java - Jmockit String final length method mocking Issue -

asp.net - Razor Page Hosted on IIS 6 Fails Every Morning -

c++ - wxwidget compiling on windows command prompt -