Re: best way to parse a simple xml string?
Michael Gilbert <[email protected]>
| Newsgroups | gmane.comp.python.xml |
|---|---|
| Message-ID | <[email protected]> |
Thank you so much Dave, This is very helpful. Have a happy holiday. Mike On 12/24/05, Dave Kuhlman <[email protected]> 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 - [email protected] > http://mail.python.org/mailman/listinfo/xml-sig > _______________________________________________ XML-SIG maillist - [email protected] http://mail.python.org/mailman/listinfo/xml-sig