xml.dom.minidom appendChild/toxml formatting problem

Discussion in 'Python' started by Ray, Nov 5, 2004.

  1. Ray

    Ray Guest

    I have put together some pretty simple code for adding and removing
    elements from an XML file, but am having a problem with toxml writing
    out the correct format after I have called appendChild on a node.

    Here is a snippet:

    if (action == 'add'):
    newCluster = domi.createElement(elementType)
    newCluster.setAttribute("Name", elementName)
    elem.appendChild(newCluster)
    else:
    elem.removeChild(childElem)

    domi.normalize()

    configFile = open(self.flist[0], "w")
    newDoc = domi.toxml()
    configFile.write(newDoc)
    configFile.close()

    Basically, if I start out with a Cluster that looks like this in the
    XML file:
    <Cluster Name="RAYTEST">
    <Cluster Name="RAYTEST2"/>
    </Cluster>

    and then use the createElement, setAttribute, and appendChild lines
    from above to add a new Cluster called "RAYTEST3" to the "RAYTEST2"
    Cluster, after calling domi.normalize(), domi.toxml(), and
    configFile.write(), the XML looks like:
    <Cluster Name="RAYTEST">
    <Cluster Name="RAYTEST2"><Cluster Name="RAYTEST3"/></Cluster>
    </Cluster>

    instead of how I would like it to look, which would be:
    <Cluster Name="RAYTEST">
    <Cluster Name="RAYTEST2">
    <Cluster Name="RAYTEST3"/>
    </Cluster>
    </Cluster>

    Or, if I append "RAYTEST3" to "RAYTEST", it comes out as:
    <Cluster Name="RAYTEST">
    <Cluster Name="RAYTEST2"/>
    <Cluster Name="RAYTEST3"/></Cluster>

    Instead of:
    <Cluster Name="RAYTEST">
    <Cluster Name="RAYTEST2"/>
    <Cluster Name="RAYTEST3"/>
    </Cluster>


    The rest of the XML file retains its original whitespace and
    linebreaks. How can I get this to format the new changes correctly?
    Calling toprettyxml instead of toxml seems to double the whitespace
    and linebreaks in the rest of the file.
     
    Ray, Nov 5, 2004
    #1
    1. Advertising

  2. Ray

    Jeremy Jones Guest

    Ray wrote:

    >I have put together some pretty simple code for adding and removing
    >elements from an XML file, but am having a problem with toxml writing
    >out the correct format after I have called appendChild on a node.
    >
    >Here is a snippet:
    >
    >if (action == 'add'):
    > newCluster = domi.createElement(elementType)
    > newCluster.setAttribute("Name", elementName)
    > elem.appendChild(newCluster)
    >else:
    > elem.removeChild(childElem)
    >
    >domi.normalize()
    >
    >
    >
    >

    <snip>

    >Or, if I append "RAYTEST3" to "RAYTEST", it comes out as:
    ><Cluster Name="RAYTEST">
    > <Cluster Name="RAYTEST2"/>
    ><Cluster Name="RAYTEST3"/></Cluster>
    >
    >Instead of:
    ><Cluster Name="RAYTEST">
    > <Cluster Name="RAYTEST2"/>
    > <Cluster Name="RAYTEST3"/>
    ></Cluster>
    >
    >
    >The rest of the XML file retains its original whitespace and
    >linebreaks. How can I get this to format the new changes correctly?
    >Calling toprettyxml instead of toxml seems to double the whitespace
    >and linebreaks in the rest of the file.
    >
    >

    I'd like to offer you a more automated method, but I can't. Perhaps
    someone else can. The only way I can think of to do it is to see what
    kind of text nodes exist around where you are inserting your elements
    and get that for formatting and insert similar text nodes before you
    insert your elements.

    Just out of curiosity, why are you interested in preserving the
    whitespace? Is this going to be something that is human read rather
    than machine read (or human read in addition to machine read)?


    Jeremy Jones
     
    Jeremy Jones, Nov 5, 2004
    #2
    1. Advertising

  3. Ray

    Uche Ogbuji Guest

    (Ray) wrote in message news:<>...

    [SNIP]

    > after calling domi.normalize(), domi.toxml(), and
    > configFile.write(), the XML looks like:
    > <Cluster Name="RAYTEST">
    > <Cluster Name="RAYTEST2"><Cluster Name="RAYTEST3"/></Cluster>
    > </Cluster>
    >
    > instead of how I would like it to look, which would be:
    > <Cluster Name="RAYTEST">
    > <Cluster Name="RAYTEST2">
    > <Cluster Name="RAYTEST3"/>
    > </Cluster>
    > </Cluster>
    >
    > Or, if I append "RAYTEST3" to "RAYTEST", it comes out as:
    > <Cluster Name="RAYTEST">
    > <Cluster Name="RAYTEST2"/>
    > <Cluster Name="RAYTEST3"/></Cluster>
    >
    > Instead of:
    > <Cluster Name="RAYTEST">
    > <Cluster Name="RAYTEST2"/>
    > <Cluster Name="RAYTEST3"/>
    > </Cluster>
    >
    >
    > The rest of the XML file retains its original whitespace and
    > linebreaks. How can I get this to format the new changes correctly?
    > Calling toprettyxml instead of toxml seems to double the whitespace
    > and linebreaks in the rest of the file.


    There are many solutions to this, all of which require that you
    properly understand what nodes are really there in a given DOM (and in
    particular, understanding exactly how text nodes work).

    One good way I've found for getting people familiar with the actual
    DOM tree structure is using a GUi tree widget. See, for instance
    listing 2 in:

    http://www.xml.com/pub/a/2003/04/09/py-xml.html

    Anyway, for the "pretty printing confused thinsg by only adding
    whitespace" complaint, I suggest you strip whitespace first. There
    are many libraries with routines for this, but you could start with
    the following cookbook recipe if you like:

    http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/303061

    Good luck.

    --
    Uche Ogbuji Fourthought, Inc.
    http://uche.ogbuji.net http://4Suite.org http://fourthought.com
    A hands-on introduction to ISO Schematron -
    http://www-106.ibm.com/developerworks/edu/x-dw-xschematron-i.html
    Schematron abstract patterns -
    http://www.ibm.com/developerworks/xml/library/x-stron.html
    Wrestling HTML (using Python) -
    http://www.xml.com/pub/a/2004/09/08/pyxml.html
    XML's growing pains - http://www.adtmag.com/article.asp?id=10196
    XMLOpen and more XML Hacks -
    http://www.ibm.com/developerworks/xml/library/x-think27.html
    A survey of XML standards -
    http://www-106.ibm.com/developerworks/xml/library/x-stand4/
     
    Uche Ogbuji, Nov 7, 2004
    #3
  4. Ray

    Ray Guest

    Thanks for the suggestions, guys. I was able to get it to do what I
    wanted by replacing my call with toprettyxml() with a call to
    xml.dom.ext.PrettyPrint().

    news:<>...
    > (Ray) wrote in message news:<>...
    >
    > [SNIP]
    >
    > > after calling domi.normalize(), domi.toxml(), and
    > > configFile.write(), the XML looks like:
    > > <Cluster Name="RAYTEST">
    > > <Cluster Name="RAYTEST2"><Cluster Name="RAYTEST3"/></Cluster>
    > > </Cluster>
    > >
    > > instead of how I would like it to look, which would be:
    > > <Cluster Name="RAYTEST">
    > > <Cluster Name="RAYTEST2">
    > > <Cluster Name="RAYTEST3"/>
    > > </Cluster>
    > > </Cluster>
    > >
    > > Or, if I append "RAYTEST3" to "RAYTEST", it comes out as:
    > > <Cluster Name="RAYTEST">
    > > <Cluster Name="RAYTEST2"/>
    > > <Cluster Name="RAYTEST3"/></Cluster>
    > >
    > > Instead of:
    > > <Cluster Name="RAYTEST">
    > > <Cluster Name="RAYTEST2"/>
    > > <Cluster Name="RAYTEST3"/>
    > > </Cluster>
    > >
    > >
    > > The rest of the XML file retains its original whitespace and
    > > linebreaks. How can I get this to format the new changes correctly?
    > > Calling toprettyxml instead of toxml seems to double the whitespace
    > > and linebreaks in the rest of the file.

    >
    > There are many solutions to this, all of which require that you
    > properly understand what nodes are really there in a given DOM (and in
    > particular, understanding exactly how text nodes work).
    >
    > One good way I've found for getting people familiar with the actual
    > DOM tree structure is using a GUi tree widget. See, for instance
    > listing 2 in:
    >
    > http://www.xml.com/pub/a/2003/04/09/py-xml.html
    >
    > Anyway, for the "pretty printing confused thinsg by only adding
    > whitespace" complaint, I suggest you strip whitespace first. There
    > are many libraries with routines for this, but you could start with
    > the following cookbook recipe if you like:
    >
    > http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/303061
    >
    > Good luck.
    >
    > --
    > Uche Ogbuji Fourthought, Inc.
    > http://uche.ogbuji.net http://4Suite.org http://fourthought.com
    > A hands-on introduction to ISO Schematron -
    > http://www-106.ibm.com/developerworks/edu/x-dw-xschematron-i.html
    > Schematron abstract patterns -
    > http://www.ibm.com/developerworks/xml/library/x-stron.html
    > Wrestling HTML (using Python) -
    > http://www.xml.com/pub/a/2004/09/08/pyxml.html
    > XML's growing pains - http://www.adtmag.com/article.asp?id=10196
    > XMLOpen and more XML Hacks -
    > http://www.ibm.com/developerworks/xml/library/x-think27.html
    > A survey of XML standards -
    > http://www-106.ibm.com/developerworks/xml/library/x-stand4/
     
    Ray, Nov 8, 2004
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Paul Boddie
    Replies:
    0
    Views:
    1,358
    Paul Boddie
    Jun 24, 2003
  2. Greg Wogan-Browne
    Replies:
    1
    Views:
    815
    Uche Ogbuji
    Jan 28, 2005
  3. Marco

    minidom appendChild confusion

    Marco, Sep 23, 2005, in forum: Python
    Replies:
    2
    Views:
    523
    Marco
    Sep 23, 2005
  4. Replies:
    3
    Views:
    541
    Stefan Behnel
    Aug 3, 2007
  5. Johannes Bauer
    Replies:
    7
    Views:
    1,071
    Johannes Bauer
    Jun 11, 2009
Loading...

Share This Page