Re: best way to parse a simple xml string?

Michael Gilbert <[email protected]>
Newsgroups gmane.comp.python.xml
Message-ID <[email protected]>
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.

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

On 12/23/05, Michael Gilbert <[email protected]> wrote:
>
> Hello all,
>
> I appologize for positing such a trivial question, but I am having trouble
> decrypting the python XML documentation.
>
> I am parsing a relatively simple strings, each of which consist of a
> single xml tag.  For example:
>
> <user first="jean" last="valjean" dob="17290101" children="1"
> hobby="stealing bread" />
>
> What is the best way to determine only the user's hobby?  pulldom, sax,
> something else?  is there guidance on using xml tools for single tags?  This
> is what I have so far
>
>   from xml.dom.pulldom import *
>   a = parseString('<user first="jean" last="valjean" dob="17290101"
> children="1" hobby="stealing bread" />')
>
> I've tried getEvent() and expandNode(), but am not getting any closer to
> accessing the content in the string.  What are your suggestions on the best
> way to retrieve the hobby?  Thank you very much any help.
>
> Hope you all have a happy holiday,
> Mike Gilbert
>
>

_______________________________________________
XML-SIG maillist  -  [email protected]
http://mail.python.org/mailman/listinfo/xml-sig
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.