"Paul McGuire" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] > <[EMAIL PROTECTED]> wrote in message > news:[EMAIL PROTECTED] <snip> > This is what I posted, but it's not what I typed. I entered some very long lines at the console, and the newsgroup software, when wrapping the text, prefixed it with '>>>', not '...'. So this looks like something that wont run. >>>> doc.childNodes[0].childNodes = sorted([n for n in >>>> doc.childNodes[0].childNodes if n.nodeType == >>>> doc.ELEMENT_NODE],key=lambda n:n.nodeName) >>>> print doc.toprettyxml() > <?xml version="1.0" ?> > <booga bar="2" foo="1"> > <blah> > goodbye > </blah> > <well> > hello > </well> > </booga> > >>>>
Here's the console session, with '...' continuation lines: >>> xmlsrc = """<booga foo="1" bar="2"> ... <well>hello</well> ... <blah>goodbye</blah> ... </booga> ... """ >>> import xml.dom.minidom >>> doc = xml.dom.minidom.parseString(xmlsrc) >>> print doc.toprettyxml() <?xml version="1.0" ?> <booga bar="2" foo="1"> <well> hello </well> <blah> goodbye </blah> </booga> >>> [n.nodeName for n in doc.childNodes] [u'booga'] >>> [n.nodeName for n in doc.childNodes[0].childNodes] ['#text', u'well', '#text', u'blah', '#text'] >>> [n.nodeName for n in doc.childNodes[0].childNodes ... if n.nodeType == doc.ELEMENT_NODE] [u'well', u'blah'] >>> doc.childNodes[0].childNodes = sorted( ... doc.childNodes[0].childNodes,key=lambda n:n.nodeName) >>> [n.nodeName for n in doc.childNodes[0].childNodes ... if n.nodeType == doc.ELEMENT_NODE] [u'blah', u'well'] >>> print doc.toprettyxml() <?xml version="1.0" ?> <booga bar="2" foo="1"> <blah> goodbye </blah> <well> hello </well> </booga> >>> doc.childNodes[0].childNodes = sorted( ... [n for n in doc.childNodes[0].childNodes ... if n.nodeType==doc.ELEMENT_NODE], ... key=lambda n:n.nodeName) >>> print doc.toprettyxml() <?xml version="1.0" ?> <booga bar="2" foo="1"> <blah> goodbye </blah> <well> hello </well> </booga> >>> -- http://mail.python.org/mailman/listinfo/python-list