Results 1 to 3 of 3

Thread: Question about python minidom from xml.dom

  1. #1

    [solved]Question about python minidom from xml.dom

    Hi all,

    I am wondering why the following returns empty results:
    print commandlet.firstChild.toxml()
    print commandlet.childNodes[0].toxml() #this is the same as firstChild
    print commandlet.childNodes[2].toxml()

    It must be something I overlook, ... but I can't put my finger on it.
    Thanks for having a look on it,

    Here's the complete picture:

    >>> from xml.dom import minidom
    >>> xmldoc = minidom.parse('/tmp/test.xml')
    >>> print xmldoc.firstChild.toxml()
    <commandlet>
    <properties>
    <version>0.1 Beta</version>
    <name>test</name>
    <description>just a test</description>
    <author>somebody</author>
    <platform>linux2</platform>
    <exitcode>0</exitcode>
    <errors/>
    <totaltime>3.19</totaltime>
    </properties>
    <grid>
    <row number="2">
    <column number="8" value="100"/>
    </row>
    <row number="3">
    <column number="8" value="97.8"/>
    </row>
    <row number="4">
    <column number="8" value="97.7"/>
    </row>
    <row number="5">
    <column number="8" value="97.6"/>
    </row>
    </grid>
    </commandlet>


    >>> commandlet=xmldoc.firstChild
    >>> print commandlet.firstChild.toxml()


    >>> print commandlet.childNodes[0].toxml()


    >>> print commandlet.childNodes[1].toxml()
    <properties>
    <version>0.1 Beta</version>
    <name>test</name>
    <description>just a test</description>
    <author>somebody</author>
    <platform>linux2</platform>
    <exitcode>0</exitcode>
    <errors/>
    <totaltime>3.19</totaltime>
    </properties>
    >>> print commandlet.childNodes[2].toxml()


    >>> print commandlet.childNodes[3].toxml()
    <grid>
    <row number="2">
    <column number="8" value="100"/>
    </row>
    <row number="3">
    <column number="8" value="97.8"/>
    </row>
    <row number="4">
    <column number="8" value="97.7"/>
    </row>
    <row number="5">
    <column number="8" value="97.6"/>
    </row>
    </grid>
    >>>
    Last edited by smetj; March 29th, 2009 at 10:13 AM.

  2. #2
    WW is offline Iced Blended Vanilla Crème Ubuntu
    Join Date
    Oct 2004
    Beans
    1,532

    Re: Question about python minidom from xml.dom

    The newlines in your XML document become Text nodes in your DOM object:
    Code:
    In [28]: commandlet.firstChild
    Out[28]: 
    <DOM Text node "
    ">
    
    In [29]: commandlet.firstChild.toxml()
    Out[29]: u'\n'
    
    In [30]: print commandlet.firstChild.toxml()
    
    
    
    In [31]: commandlet.childNodes[0]
    Out[31]: 
    <DOM Text node "
    ">
    
    In [32]: commandlet.childNodes[1]
    Out[32]: <DOM Element: properties at 0x1524468>
    
    In [33]: commandlet.childNodes[2]
    Out[33]: 
    <DOM Text node "
    ">
    
    In [34]: commandlet.childNodes[3]
    Out[34]: <DOM Element: grid at 0x1524a08>
    
    In [35]:
    See how the first and third children of commandlet are Text nodes? In sample statements that you used, the functions are not returning empty results. The print statements are simply printing the single newline character in the Text nodes.
    Last edited by WW; March 15th, 2009 at 05:28 PM.

  3. #3

    Re: Question about python minidom from xml.dom

    Excellent! It's so obvious
    Thanks!

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •