# REXML - controlling whitespaces and inserting CR

**URL:** <https://www.ruby-forum.com/t/rexml-controlling-whitespaces-and-inserting-cr/192534>\
**Category:** Ruby\
**Created:** [August 1, 2010, 4:40pm UTC](https://www.ruby-forum.com/t/rexml-controlling-whitespaces-and-inserting-cr/192534 "2010-08-01T16:40:54Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![ctwise](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/a183cd/32.png) [@ctwise](https://www.ruby-forum.com/u/ctwise)\
**Post date:** [August 1, 2010, 4:40pm UTC](https://www.ruby-forum.com/t/rexml-controlling-whitespaces-and-inserting-cr/192534/1 "2010-08-01T16:40:54Z")

</div>

Hello All,

I have an XML file containing this  
  
  
Description de  
Description en  
  
  
Description de  
Description en

I made a code to add a new tag to each .  
The part of code is:

require “rexml/document”  
include REXML  
…  
…  
pval = Element.new(“value”)  
pval.add\_attribute(“xml:lang”,“fr”)  
pval.add\_text(“Description fr”)  
fproperty.elements \<\< pval  
fproperty is the node object

My final XML file looks like this:

 Description de Description en Description fr Description de Description en Description fr

One “TAB” is inserted for each 4 spaces which were in the original file.  
How to avoid this?  
And how to add a CR to the new element?

Thanks for your help, I’m a very newby

---

<div class="post-metadata">

**Author:** ![ctwise](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/a183cd/32.png) [@ctwise](https://www.ruby-forum.com/u/ctwise)\
**Post date:** [August 1, 2010, 9:07pm UTC](https://www.ruby-forum.com/t/rexml-controlling-whitespaces-and-inserting-cr/192534/2 "2010-08-01T21:07:27Z")

</div>

2010/8/1 Charles W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> ```
> <value xml:lang="en">Description en</value>
> pval = Element.new("value")
> 
> ```
> 
> Description en
> 
> Thanks for your help, I’m a very newby

I think, you will have to create your own formatter - or use a  
different one for output. You’ll find some explanation in the REXML  
documentation.

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![ctwise](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/a183cd/32.png) [@ctwise](https://www.ruby-forum.com/u/ctwise)\
**Post date:** [August 1, 2010, 9:33pm UTC](https://www.ruby-forum.com/t/rexml-controlling-whitespaces-and-inserting-cr/192534/3 "2010-08-01T21:33:53Z")

</div>

Robert K. wrote:

> I think, you will have to create your own formatter - or use a  
> different one for output. You’ll find some explanation in the REXML  
> documentation.

Thank you Robert for your reply.  
I am willing to write my own formatter but despite I read some REXML  
docs, I don’t know where to begin. 😃

This is really my first day with ruby;  
concerning REXML I’ll be happy to be pointed to some samples.

In advance Thanks to All,

Charles

---

<div class="post-metadata">

**Author:** ![ctwise](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/a183cd/32.png) [@ctwise](https://www.ruby-forum.com/u/ctwise)\
**Post date:** [August 12, 2010, 7:11pm UTC](https://www.ruby-forum.com/t/rexml-controlling-whitespaces-and-inserting-cr/192534/4 "2010-08-12T19:11:59Z")

</div>

2010/8/1 Charles W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> This is really my first day with ruby;  
> concerning REXML I’ll be happy to be pointed to some samples.

I hope you found it by now. If not, please see  
[http://www.germane-software.com/software/rexml/docs/tutorial.html](http://www.germane-software.com/software/rexml/docs/tutorial.html)

Cheers

robert

---

<div class="post-metadata">

**Author:** ![ctwise](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/a183cd/32.png) [@ctwise](https://www.ruby-forum.com/u/ctwise)\
**Post date:** [August 12, 2010, 9:25pm UTC](https://www.ruby-forum.com/t/rexml-controlling-whitespaces-and-inserting-cr/192534/5 "2010-08-12T21:25:20Z")

</div>

Robert K. wrote:

> I hope you found it by now. If not, please see  
> [http://www.germane-software.com/software/rexml/docs/tutorial.html](http://www.germane-software.com/software/rexml/docs/tutorial.html)

I saw it before but I’ll give it a look again.  
Great thank you.  
Charles

---

<div class="post-metadata">

**Author:** ![ctwise](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/a183cd/32.png) [@ctwise](https://www.ruby-forum.com/u/ctwise)\
**Post date:** [August 12, 2010, 9:44pm UTC](https://www.ruby-forum.com/t/rexml-controlling-whitespaces-and-inserting-cr/192534/6 "2010-08-12T21:44:48Z")

</div>

Afternoon,

You might find it just as easy to take that output and push it out to a  
file  
and then hit it with xmlindent ([http://xmlindent.sourceforge.net/](http://xmlindent.sourceforge.net/)) it  
will  
do exactly what you want. No sense in re-inventing the wheel.

John

---

<div class="post-metadata">

**Author:** ![ctwise](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/a183cd/32.png) [@ctwise](https://www.ruby-forum.com/u/ctwise)\
**Post date:** [August 12, 2010, 10:36pm UTC](https://www.ruby-forum.com/t/rexml-controlling-whitespaces-and-inserting-cr/192534/7 "2010-08-12T22:36:29Z")

</div>

John W Higgins wrote:

> Afternoon,
> 
> You might find it just as easy to take that output and push it out to a  
> file  
> and then hit it with xmlindent ([http://xmlindent.sourceforge.net/](http://xmlindent.sourceforge.net/)) it  
> will  
> do exactly what you want. No sense in re-inventing the wheel.

Yes John.  
It seems like it will ideally fit my need.

Thanks a lot.  
Charles
