How do I get the value out of a DOM Element

Paul Boddie paul at boddie.org.uk
Thu Sep 27 04:27:58 EDT 2007


On 27 Sep, 07:50, kj7ny <kj... at nakore.com> 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?

DOM Level 3 provides the textContent property:

http://www.w3.org/TR/DOM-Level-3-Core/core.html#Node3-textContent

You'll find this in libxml2dom and possibly some other packages such
as pxdom. For the above case with minidom specifically (at least with
versions I've used), you need to iterate over the childNodes of the
element, obtaining the nodeValue for each node and joining them
together. Something like this might do it:

"".join([n.nodeValue for n in element.childNodes])

It's not pretty, but encapsulating stuff like this is what functions
are good for.

Paul




More information about the Python-list mailing list