minidom
Peter Hansen
peter at engcorp.com
Tue Dec 4 02:40:05 EST 2001
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:
>>> from xml.dom import minidom
>>> doc = minidom.parseString(open('test.xml').read())
>>> doc.getElementsByTagName('DESCRIPTION')[0].childNodes[0].nodeValue
u'EngineStart Leak'
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