How do I get the value out of a DOM Element

Stefan Behnel stefan.behnel-n05pAM at web.de
Thu Sep 27 09:28:14 CEST 2007


kj7ny wrote:
> I have been able to get xml.dom.minidom.parse('somefile.xml') and then
> dom.getElementsByTagName('LLobjectID') to work to the point where I
> get something like: [<DOM Element: LLobjectID at 0x13cba08>] which I
> can get down to <DOM Element: LLobjectID at 0x13cba08> but then I
> can't find any way to just get the value out from the thing!
> 
> .toxml() returns something like: u'<LLobjectID><![CDATA[1871203]]></
> LLobjectID>'.
> 
> How do I just get the 1871203 out of the DOM Element?

It contains a CDATA node which in turn contains a Text node (AFAIR), so you
have to walk through the children to get what you want.

Alternatively, try an XML API that makes it easy to handle XML, like
ElementTree (part of the stdlin in Python 2.5) or lxml, both of which have
compatible APIs. The code would look like this:

   tree = etree.parse("some_file.xml")
   id = tree.find("//LLobjectID")
   print id.text

Stefan



More information about the Python-list mailing list