how to print newline in xml?

Discussion in 'Python' started by anatoli.barski@googlemail.com, May 30, 2006.

  1. Guest

    I currently use xml.dom.minidom and ext to create a dom-tree which I
    would write to an xml-file.
    My intention is to create something like this:

    <Robot> <!-- armar3 -->
    <Joint>
    </Joint>

    <Joint>
    </Joint>
    </Robot>

    but I didn't find any possibility either to put comment directly behind
    a tag nor did I found how to put a new line like in example between the
    two joints. The reason why I'm trying to achieve this: the file should
    be easy to read for a human...

    now I can just print the upper document like this:

    <!-- armar3 -->
    <Robot>
    <Joint>
    </Joint>
    <Joint>
    </Joint>
    </Robot>

    which is already not very readable - but it becomes even worse along
    with increasing number of comments
    , May 30, 2006
    #1
    1. Advertising

  2. John Bokma Guest

    "" <> wrote:

    > which is already not very readable - but it becomes even worse along
    > with increasing number of comments


    How about:

    <robot>
    <description>armar3</description>
    :
    :
    </robot>

    XML editor + xpath makes this way more easier to use by humans compared to
    normal comments. Also, if you declare several of comment elements
    (summary, description, author, etc.) you can auto generate documentation
    etc.

    --
    John MexIT: http://johnbokma.com/mexit/
    personal page: http://johnbokma.com/
    Experienced programmer available: http://castleamber.com/
    Happy Customers: http://castleamber.com/testimonials.html
    John Bokma, May 31, 2006
    #2
    1. Advertising

  3. Guest

    Thank you for a possible solution, but it's not what I'm looking for,
    cause something like <!-- comment --> would look quite similar... for
    big elements like robot it would be ok to use comment as a child of
    element - but imagine I'd like to comment transformation:

    <transformation x="0" y="0" z="0"/> I wouldn't like to make this
    element parent of a description - it is not nice - I have lots of such
    elements.

    It is important that a user after having a glance at the document is
    able to quickly find and change some values. I don't want to bother him
    with reading documentation nodes - but if he needs - they should be
    there... the only possible solution I can think of would be:

    <Robot>
    <verbose mode="enabled"/> <!-- enabled, disabled
    -->
    <CollisionChecker type="vcollide"/> <!-- possible values for
    type: vcollide, (nyi: disabled) -->
    <Visualisation mode="3DModel"/> <!-- posiible values for
    mode: 3DModel, (nyi: disabled, line) -->

    <!-- <Offset eulerX="-90" eulerZ="90"/> -->

    <RootJoint> <!-- the one and only
    root joint -->
    <Name value="Armar3"/> <!-- name of this joint
    -->
    <DH> <!-- Denavit Hartenberg
    parameters -->
    <alpha value="0"/>
    </DH>

    <ID value="0"/> <!-- unique ID of this
    joint -->
    <ChildNodes childID="1"/> <!-- all child joints of
    this joint -->
    </RootJoint>

    <ChildJoint>
    <Name value="Platform"/>
    <JointTransformation>
    <RotationAxis X="0" Y="0" Z="1" INIT="0"/>
    <TranslationFromParent X="0" Y="0" Z="0"/>
    </JointTransformation>
    <Visualisation> <!-- visualisation
    settings for this joint -->
    <IVModel file="model/platform.iv"/> <!-- filename of full 3d
    model -->
    </Visualisation>
    <ID value="1"/> <!-- unique ID of this
    joint -->
    <ChildNodes childID="2"/> <!-- all child joints of
    this joint -->
    </ChildJoint>

    > How about:
    >
    > <robot>
    > <description>armar3</description>
    > :
    > :
    > </robot>
    >
    > XML editor + xpath makes this way more easier to use by humans compared to
    > normal comments. Also, if you declare several of comment elements
    > (summary, description, author, etc.) you can auto generate documentation
    > etc.
    >
    > --
    > John MexIT: http://johnbokma.com/mexit/
    > personal page: http://johnbokma.com/
    > Experienced programmer available: http://castleamber.com/
    > Happy Customers: http://castleamber.com/testimonials.html
    , Jun 1, 2006
    #3
  4. John Bokma Guest

    "" <> wrote:

    > Thank you for a possible solution, but it's not what I'm looking for,
    > cause something like <!-- comment --> would look quite similar... for
    > big elements like robot it would be ok to use comment as a child of
    > element - but imagine I'd like to comment transformation:
    >
    > <transformation x="0" y="0" z="0"/> I wouldn't like to make this
    > element parent of a description - it is not nice - I have lots of such
    > elements.


    Then add the comment to the parent of transformation, or an other option
    might be:

    <transformation x="0" y="0" z="0" description="some description"/>

    > It is important that a user after having a glance at the document is
    > able to quickly find and change some values. I don't want to bother
    > him with reading documentation nodes - but if he needs - they should
    > be there... the only possible solution I can think of would be:
    >
    > <Robot>
    > <verbose mode="enabled"/> <!-- enabled, disabled
    > -->


    Several of your comments are stating the obvious, it's like:

    x++ increments the value x contains with one


    > <DH> <!-- Denavit Hartenberg


    Picking a better name would solve that problem. DenavitHartenberg might be
    a bit long though.


    BTW: don't top post.

    --
    John MexIT: http://johnbokma.com/mexit/
    personal page: http://johnbokma.com/
    Experienced programmer available: http://castleamber.com/
    Happy Customers: http://castleamber.com/testimonials.html
    John Bokma, Jun 1, 2006
    #4
  5. Guest

    Thank you! I think description as an attribute is readable. But now I'm
    thinking about the order of attributes cause I noticed that for
    instance X="0" Y="0" Z="0" set in this order by my script is printed
    like X="0" Z="0" Y="0" therefore it could be messy - I wouldn't like
    the description to be somewhere between the other attributes. But I
    have to test it.

    I'll have to consider some better names for the elements

    Sending my regards, Anatoli
    , Jun 1, 2006
    #5
  6. John Bokma Guest

    "" <> wrote:

    > Thank you! I think description as an attribute is readable. But now I'm
    > thinking about the order of attributes cause I noticed that for
    > instance X="0" Y="0" Z="0" set in this order by my script is printed
    > like X="0" Z="0" Y="0" therefore it could be messy - I wouldn't like
    > the description to be somewhere between the other attributes. But I
    > have to test it.


    I don't know the library you are using, and also have way more experience
    in Perl. I guess that the attributes are stored in a dictionary, which has
    no order, and hence the original order of the attributes is lost. In Perl
    with some modules I use I can chose to use a list or a hash (=
    dictionary). With the former the original order is preserved.

    I am sure something like that is possible in Python as well.


    --
    John MexIT: http://johnbokma.com/mexit/
    personal page: http://johnbokma.com/
    Experienced programmer available: http://castleamber.com/
    Happy Customers: http://castleamber.com/testimonials.html
    John Bokma, Jun 1, 2006
    #6
  7. Guest

    I use Python/XML packages are xml.dom.minidom and xml.dom.ext (second
    just for PrettyPrint)
    , Jun 1, 2006
    #7
  8. Guest

    wrote:
    > I use Python/XML packages are xml.dom.minidom and xml.dom.ext (second
    > just for PrettyPrint)


    You don't need xml.dom.ext for prettyprint. You can use
    doc.toprettyxml()

    I gather you want to tweak the prettyprinter to not add the newline
    before the comment. The only way to do this is to write your own
    printing logic, which is really not that hard, if you just start by
    copying the code from .writexml (used by .toprettyxml).

    But there's an even easier (if slower) way: pretty print the document,
    then parse it in again, remove the text node between the element in
    question and the following comment, and then use .writexml() to
    serialize it it again.

    A few general notes:

    * You cannot set the order of attributes in most XML tools, whether
    Python or not. This is unfortunate for people who would like to
    preserve such details for usability reasons, but that's just the way
    XML is. The closest you can get is by using canonicalization [1],
    which is available in PyXML as xml.dom.ext.c14n. It just so happens
    that canonical XML leaves the attributes in the order you want. You
    won't always be so lucky.

    * You can always create text nodes by using doc.createTextNode.

    * You can always remove text nodes (or any other kind) by using
    ..removeChild

    * It's much easier to navigate if you use XPath. PyXML has an
    xml.xpath module you can use.

    Good luck.

    [1] http://www-128.ibm.com/developerworks/xml/library/x-c14n/

    --
    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/
    , Jun 4, 2006
    #8
    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. Tobiah
    Replies:
    5
    Views:
    313
    Erik Max Francis
    Sep 16, 2003
  2. Replies:
    4
    Views:
    552
    Chris Herborth
    May 28, 2004
  3. David Stockwell

    RE: How to I print without newline ?

    David Stockwell, May 27, 2004, in forum: Python
    Replies:
    0
    Views:
    3,460
    David Stockwell
    May 27, 2004
  4. keto
    Replies:
    0
    Views:
    884
  5. David Cournapeau

    print a vs print '%s' % a vs print '%f' a

    David Cournapeau, Dec 30, 2008, in forum: Python
    Replies:
    0
    Views:
    330
    David Cournapeau
    Dec 30, 2008
Loading...

Share This Page