minidom

Tom Good Tom_Good1 at excite.com
Tue Dec 4 19:53:14 EST 2001


Peter Hansen <peter at engcorp.com> wrote in message news:<3C0CE9DA.128BBF4A at engcorp.com>...
> Martin von Loewis wrote:
> > 
> > Peter Hansen <peter at engcorp.com> writes:
> > 
> > > 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.)
> > 
> > That is not true. Presence of the XML declaration is not a validity
> > constraint in XML.
> 
> Is it not required in any way at all?  I always thought 
> it was.
> 
> > > >>> from xml.dom import minidom
> > > >>> doc = minidom.parseString(open('test.xml').read())
> > > >>> doc.getElementsByTagName('DESCRIPTION')[0].childNodes[0].nodeValue
> > > u'EngineStart Leak'
> > 
> > Actually, obtaining the text by looking at the first child is
> > unreliable: The first child may be a comment, and the text may be
> 
> Yes, this was not intended to be good style, or generally useful,
> but merely an inspiration to the OP to use the interactive
> console as a start in learning more about the API.  I could 
> have made that clearer.

Maybe something like this could be used to be more general...

e = doc.getElementsByTagName('DESCRIPTION')[0]
alltext = [a.nodeValue for a in e.childNodes if a.nodeType == a.TEXT_NODE]
string.join(alltext)



More information about the Python-list mailing list