how to print newline in xml?

A

anatoli.barski

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
 
J

John Bokma

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.
 
A

anatoli.barski

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 -->
 
J

John Bokma

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:

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.
 
A

anatoli.barski

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
 
J

John Bokma

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.
 
A

anatoli.barski

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

uche.ogbuji

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/
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top