[XML-SIG] best way to parse a simple xml string?
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.
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
> > 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"
> > hobby="stealing bread" />'
> > dom = xml.dom.minidom.parseString(document)
> > t = dom.getElementsByTagName("user")
> > 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
> XML-SIG maillist - XML-SIG at python.org
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the XML-SIG