minidom

Duncan Smith buzzard at urubu.freeserve.co.uk
Tue Dec 4 05:34:31 EST 2001


"Peter Hansen" <peter at engcorp.com> wrote in message
news:3C0C7DD5.AC50A8CB at engcorp.com...
> Duncan Smith wrote:
> >
> > This may be a stupid question, but I'm struggling.  I can retreive
> > information from an XML file (using minidom) as long as it is a node
> > attribute.  But I cannot figure out how to get data otherwise.  eg.
> >
> > <VAR NAME="ESLeak" TYPE="discrete" XPOS="16030" YPOS="16030">
> >
> >     <DESCRIPTION>EngineStart Leak</DESCRIPTION>
> >
> >     <STATENAME>good</STATENAME>
> >
> >     <STATENAME>bad</STATENAME>
> >
> > XPOS etc. no problem.  But how (using minidom) do I retreive 'Engine
Start
> > Leak' and 'good' etc?  Anyone know of any good documentation / worked
> > examples for minidom?  Thanks in advance.
>
> For one, you should probably start your XML document with
> <?xml version="1.0"?> or it won't be valid XML (but that's
> not necessary for this case.)  You also need to close
> the <VAR> tag at the end with </VAR> or you'll get a
> parse error (SAXParseException).  Having done that:
>

Yes.  It was just a snippet pasted from an existing XML document.  I should
have tidied it up.

> >>> from xml.dom import minidom
> >>> doc = minidom.parseString(open('test.xml').read())
> >>> doc.getElementsByTagName('DESCRIPTION')[0].childNodes[0].nodeValue
> u'EngineStart Leak'
>

OK.  I'll give that a go.  I was using 'doc = xml.dom.minidom.parse(f)' and
getting 'None' returned from 'nodeValue'.  Cheers.

Duncan

> That should give you a start.  Use the interactive prompt
> and use dir() a lot.  For example:
>
> >>> dir(doc)
> ['childNodes', 'doctype', 'implementation', 'ownerDocument', 'parentNode']
> >>> dir(doc.childNodes[0])
> ['_attrs', '_attrsNS', 'childNodes', 'localName', 'namespaceURI',
'nextSibling',
>  'nodeName', 'nodeValue', 'ownerDocument', 'parentNode', 'prefix',
>  'previousSibling', 'tagName']
>
> The API is effectively the DOM standard, so you shouldn't have
> much trouble if you have a reference to that handy.
>
> --
> ----------------------
> Peter Hansen, P.Eng.
> peter at engcorp.com





More information about the Python-list mailing list