XML parser that sorts elements?

Paul McGuire ptmcg at austin.rr._bogus_.com
Fri Sep 22 18:27:02 CEST 2006


"Paul McGuire" <ptmcg at austin.rr._bogus_.com> wrote in message 
news:_OTQg.4326$nX4.2493 at tornado.texas.rr.com...
> <jmike at alum.mit.edu> wrote in message 
> news:1158939240.213404.239690 at m73g2000cwd.googlegroups.com...
<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>

>>>






More information about the Python-list mailing list