[XML-SIG] best way to parse a simple xml string?

Dave Kuhlman dkuhlman at cutter.rexx.com
Sat Dec 24 06:27:25 CET 2005

On Fri, Dec 23, 2005 at 03:53:33PM -0500, Michael Gilbert wrote:
> Hello again,
> I think I found a way to accomplish my goal with minidom.  Is this the most
> direct solution for my goal, or is there a simpler way?  Thanks again.

minidom is a good choice because it is part of the standard Python
library.  If you, or your users, are willing to install extra
software, you may want to look at ElementTree and lxml:

- ElementTree: http://effbot.org/zone/element-index.htm

- lxml: http://codespeak.net/lxml/

They are DOM-like, but some consider it a better DOM.

> import xml.dom.minidom
> document = '<user first="jean" last="valjean" dob="17290101" children="1"
> hobby="stealing bread" />'
> dom = xml.dom.minidom.parseString(document)
> t = dom.getElementsByTagName("user")[0]
> if t.hasAttributes():
>     for cnt in range(0, t.attributes.length):
>         if t.attributes.item(cnt).nodeName == "hobby":
>             print 'hobby = ' + t.attributes.item(cnt).nodeValue

Yes.  But, there may be a slightly more direct way.  minidom
attributes are a NamedNodeMap which is a sort of dictionary-like
object.  So you can use indexing, for example, in your case,
something like:


and also:

    if t.attributes.has_key('hobby'):
        val = t.attributes['hobby']

Use dir(t.attributes) to get a list of other methods.



Dave Kuhlman

More information about the XML-SIG mailing list