joedborg
joedborg

Reputation: 18353

Handling XML with Python

All I want to do is get the content of an XML tag in Python. I'm maybe using the wrong import; ideally I'd love to have the way PHP deals with XML (i.e $XML->this_tag), like the way pyodbc does database stuff (i.e. table.field)

Here's my example:

from xml.dom.minidom import parseString
dom = parseString("<test>I want to read this</test>")
dom.getElementsByTagName("test")[0].toxml()
>>> u'<test>I want to read this</test>'

All I want to be able to do read the contents of the tag (like innerHTML in javascript).

Upvotes: 0

Views: 751

Answers (3)

mdeous
mdeous

Reputation: 18029

Use firstChild.data instead of toxml:

from xml.dom.minidom import parseString

dom = parseString('<test>I want to read this</test>')
element = dom.getElementsByTagName('test')[0]
print element.firstChild.data

Output:

>>> I want to read this

Upvotes: 1

WeaselFox
WeaselFox

Reputation: 7380

I like BeautifulSoup :

from BeautifulSoup import BeautifulStoneSoup
xml = """<test>I want to read this</test>"""
soup = BeautifulStoneSoup(xml)
soup.find('test')

I want to read this

looks somewhat better.

Upvotes: 2

thavan
thavan

Reputation: 2429

instead of dom.getElementsByTagName("test")[0].toxml() put dom.getElementsByTagName("test")[0].firstChild.data It will print the node value.

Upvotes: 2

Related Questions