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

Michael Gilbert michael.s.gilbert at gmail.com
Sat Dec 24 15:43:49 CET 2005


Thank you so much Dave,

This is very helpful.  Have a happy holiday.

Mike

On 12/24/05, Dave Kuhlman <dkuhlman at cutter.rexx.com> wrote:
>
> 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:
>
>     t.attributes['hobby']
>
> and also:
>
>     if t.attributes.has_key('hobby'):
>         val = t.attributes['hobby']
>
> Use dir(t.attributes) to get a list of other methods.
>
> Dave
>
> [snip]
>
> --
> Dave Kuhlman
> http://www.rexx.com/~dkuhlman
> _______________________________________________
> XML-SIG maillist  -  XML-SIG at python.org
> http://mail.python.org/mailman/listinfo/xml-sig
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/xml-sig/attachments/20051224/5b027655/attachment.html


More information about the XML-SIG mailing list