Fromatting an xml file

Discussion in 'Python' started by sir_alex, Feb 2, 2006.

  1. sir_alex

    sir_alex Guest

    Hi! I have a little problem writing xml files formatted in a way like
    the following:

    <rootnode>
    <node>bla</node>
    <node>bla</node>
    </rootnode>

    Every new node element should have a tabulation before it, but when I
    use xml.dom.minidom I use writexml, which considers as a new node also
    the text (in my little example, "bla" phrases), so the best result I
    achieved has been the following

    <rootnode>
    <node>
    bla
    </node>
    </rootnode>

    but I don't want the text to be written on newlines... is there a good
    solution? Thanks!
     
    sir_alex, Feb 2, 2006
    #1
    1. Advertising

  2. sir_alex

    Harry George Guest

    "sir_alex" <> writes:

    > Hi! I have a little problem writing xml files formatted in a way like
    > the following:
    >
    > <rootnode>
    > <node>bla</node>
    > <node>bla</node>
    > </rootnode>
    >
    > Every new node element should have a tabulation before it, but when I
    > use xml.dom.minidom I use writexml, which considers as a new node also
    > the text (in my little example, "bla" phrases), so the best result I
    > achieved has been the following
    >
    > <rootnode>
    > <node>
    > bla
    > </node>
    > </rootnode>
    >
    > but I don't want the text to be written on newlines... is there a good
    > solution? Thanks!
    >


    Based on recommendations from this newsgroup, I write XML directly,
    instead of going through DOM. That in turn requires a mechanism for
    tabbed indents, so I wrote tabbedwriter:

    http://www.seanet.com/~hgg9140/comp/index.html
    http://www.seanet.com/~hgg9140/comp/tabbedwriter/doc/tabbedwriter.help


    --
    6-6M21 BCA CompArch Design Engineering
    Phone: (425) 294-4718
     
    Harry George, Feb 3, 2006
    #2
    1. Advertising

  3. sir_alex

    Guest

    sir_alex wrote:
    > Hi! I have a little problem writing xml files formatted in a way like
    > the following:
    >
    > <rootnode>
    > <node>bla</node>
    > <node>bla</node>
    > </rootnode>
    >
    > Every new node element should have a tabulation before it, but when I
    > use xml.dom.minidom I use writexml, which considers as a new node also
    > the text (in my little example, "bla" phrases), so the best result I
    > achieved has been the following
    >
    > <rootnode>
    > <node>
    > bla
    > </node>
    > </rootnode>
    >
    > but I don't want the text to be written on newlines... is there a good
    > solution? Thanks!


    That minidom behavior is fairly unsafe. 4Suite's PrettyPrinter is much
    safer:

    >>> from Ft.Xml import Parse
    >>> from Ft.Xml.Domlette import PrettyPrint
    >>> XML = "<rootnode><node>bla</node><node>bla</node></rootnode>"
    >>> doc = Parse(XML)
    >>> PrettyPrint(doc)

    <?xml version="1.0" encoding="UTF-8"?>
    <rootnode>
    <node>bla</node>
    <node>bla</node>
    </rootnode>
    >>>


    http://4Suite.org

    --
    Uche Ogbuji Fourthought, Inc.
    http://uche.ogbuji.net http://fourthought.com
    http://copia.ogbuji.net http://4Suite.org
    Articles: http://uche.ogbuji.net/tech/publications/
     
    , Feb 7, 2006
    #3
    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 Say

    DataList Fromatting Cell

    Paul Say, Jun 24, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    384
    Eliyahu Goldin
    Jun 26, 2005
  2. jkflens
    Replies:
    2
    Views:
    1,497
    jkflens
    May 30, 2006
  3. gt
    Replies:
    4
    Views:
    328
  4. Lit
    Replies:
    4
    Views:
    359
  5. Erik Wasser
    Replies:
    5
    Views:
    463
    Peter J. Holzer
    Mar 5, 2006
Loading...

Share This Page