Hulk
Hulk

Reputation: 34160

Accessing child nodein an xml in python

How to retrieve the value of type in the below XML

 <info><category>Flip</category><info>2</info><type>Tree</type></info>

Upvotes: 1

Views: 95

Answers (1)

codeape
codeape

Reputation: 100756

Using ElementTree:

import xml.etree.ElementTree as E
e = E.parse("test.xml")
print(e.find("type").text)

Using minidom:

import xml.dom.minidom
d = xml.dom.minidom.parse("test.xml")
print(d.getElementsByTagName("type")[0].firstChild.data)

Using BeautifulSoup:

from BeautifulSoup import BeautifulStoneSoup
soup = BeautifulStoneSoup(open("test.xml"))
print(soup.find("type").text)

Upvotes: 2

Related Questions