how to print newline in xml?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • anatoli.barski@googlemail.com

    how to print newline in xml?

    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

  • John Bokma

    #2
    Re: how to print newline in xml?

    "anatoli.barski @googlemail.com " <anatoli.barski @googlemail.com > wrote:
    [color=blue]
    > which is already not very readable - but it becomes even worse along
    > with increasing number of comments[/color]

    How about:

    <robot>
    <description>ar mar3</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

    Comment

    • anatoli.barski@googlemail.com

      #3
      Re: how to print newline in xml?

      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:

      <transformati on 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
      -->
      <CollisionCheck er type="vcollide"/> <!-- possible values for
      type: vcollide, (nyi: disabled) -->
      <Visualisatio n 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 "/>
      <JointTransform ation>
      <RotationAxis X="0" Y="0" Z="1" INIT="0"/>
      <TranslationFro mParent X="0" Y="0" Z="0"/>
      </JointTransforma tion>
      <Visualisatio n> <!-- 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>
      [color=blue]
      > How about:
      >
      > <robot>
      > <description>ar mar3</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[/color]

      Comment

      • John Bokma

        #4
        Re: how to print newline in xml?

        "anatoli.barski @googlemail.com " <anatoli.barski @googlemail.com > wrote:
        [color=blue]
        > 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:
        >
        > <transformati on 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.[/color]

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

        <transformati on x="0" y="0" z="0" description="so me description"/>
        [color=blue]
        > 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
        > -->[/color]

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

        x++ increments the value x contains with one

        [color=blue]
        > <DH> <!-- Denavit Hartenberg[/color]

        Picking a better name would solve that problem. DenavitHartenbe rg 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

        Comment

        • anatoli.barski@googlemail.com

          #5
          Re: how to print newline in xml?

          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

          Comment

          • John Bokma

            #6
            Re: how to print newline in xml?

            "anatoli.barski @googlemail.com " <anatoli.barski @googlemail.com > wrote:
            [color=blue]
            > 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.[/color]

            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

            Comment

            • anatoli.barski@googlemail.com

              #7
              Re: how to print newline in xml?

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

              Comment

              • uche.ogbuji@gmail.com

                #8
                Re: how to print newline in xml?

                anatoli.barski@ googlemail.com wrote:[color=blue]
                > I use Python/XML packages are xml.dom.minidom and xml.dom.ext (second
                > just for PrettyPrint)[/color]

                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 canonicalizatio n [1],
                which is available in PyXML as xml.dom.ext.c14 n. 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.createTextN ode.

                * 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/developerwork...ibrary/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/

                Comment

                Working...