0

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).

2
  • maybe try dom.getElementsByTagName("test")[0].firstChild().toxml() Commented Jan 12, 2012 at 12:34
  • 2
    There is a useful summary of python xml support on the python wiki Commented Jan 12, 2012 at 12:49

3 Answers 3

2

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

Sign up to request clarification or add additional context in comments.

Comments

2

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.

1 Comment

Like it! Will use that in a future project!
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.