# REXML and Empty-Elements

**URL:** <https://rubytalk.org/t/rexml-and-empty-elements/49738>\
**Category:** ruby-talk\
**Created:** [20 October 2008 23:04 UTC](https://rubytalk.org/t/rexml-and-empty-elements/49738 "2008-10-20T23:04:13Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ryan\_Fitz](https://avatars.discourse-cdn.com/v4/letter/r/6bbea6/32.png) [@Ryan\_Fitz](https://rubytalk.org/u/Ryan_Fitz)\
**Post date:** [20 October 2008 23:04 UTC](https://rubytalk.org/t/rexml-and-empty-elements/49738/1 "2008-10-20T23:04:13Z")

</div>

Hello all,

I have been using REXML to help me merge some XML files. I noticed that  
simply opening an XML document and writing it to a file will  
automatically convert the blank nodes with a start-tag and an end-tag to  
empty-element tags. Here is an example:

Original.xml:  
\<User\>  
&nbsp;&nbsp;\<Name\>John\</Name\>  
&nbsp;&nbsp;\<Address1\>123 Maple Lane\</Address1\>  
&nbsp;&nbsp;\<Address2\>\</Address2\>  
&nbsp;&nbsp;\<City\>Chicago\</City\>  
&nbsp;&nbsp;\<State\>IL\</State\>  
\</User\>

Basic Ruby Script:  
require 'rexml/document'  
xml = REXML::Document.new(File.open("Original.xml"))  
f = File.new("New.xml","w")  
f.puts xml

New.xml:  
\<User\>  
&nbsp;&nbsp;\<Name\>John\</Name\>  
&nbsp;&nbsp;\<Address1\>123 Maple Lane\</Address1\>  
&nbsp;&nbsp;\<Address2 /\>  
&nbsp;&nbsp;\<City\>Chicago\</City\>  
&nbsp;&nbsp;\<State\>IL\</State\>  
\</User\>

In this example I would like to prevent Address2 from being modified to  
use empty-element tags. I would rather it be displayed as  
\<Address2\>\</Address2\>. Is there a way to prevent REXML from converting  
blank nodes?

Thank you for your time.

Ryan

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Robert\_K1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/robert_k1/32/1830_2.png) [@Robert\_K1](https://rubytalk.org/u/Robert_K1)\
**Post date:** [21 October 2008 06:13 UTC](https://rubytalk.org/t/rexml-and-empty-elements/49738/2 "2008-10-21T06:13:42Z")

</div>

> Hello all,
> 
> I have been using REXML to help me merge some XML files. I noticed that  
> simply opening an XML document and writing it to a file will  
> automatically convert the blank nodes with a start-tag and an end-tag to  
> empty-element tags. Here is an example:
> 
> Original.xml:  
> \<User\>  
> &nbsp;&nbsp;\<Name\>John\</Name\>  
> &nbsp;&nbsp;\<Address1\>123 Maple Lane\</Address1\>  
> &nbsp;&nbsp;\<Address2\>\</Address2\>  
> &nbsp;&nbsp;\<City\>Chicago\</City\>  
> &nbsp;&nbsp;\<State\>IL\</State\>  
> \</User\>
> 
> Basic Ruby Script:  
> require 'rexml/document'  
> xml = REXML::Document.new(File.open("Original.xml"))  
> f = File.new("New.xml","w")  
> f.puts xml

Better use the block form of File.open here.

> New.xml:  
> \<User\>  
> &nbsp;&nbsp;\<Name\>John\</Name\>  
> &nbsp;&nbsp;\<Address1\>123 Maple Lane\</Address1\>  
> &nbsp;&nbsp;\<Address2 /\>  
> &nbsp;&nbsp;\<City\>Chicago\</City\>  
> &nbsp;&nbsp;\<State\>IL\</State\>  
> \</User\>
> 
> In this example I would like to prevent Address2 from being modified to  
> use empty-element tags. I would rather it be displayed as  
> \<Address2\>\</Address2\>. Is there a way to prevent REXML from converting  
> blank nodes?

There is no point in doing this since both representations are equivalent from an XML point of view. I don't believe there is such a mechanism. What does REXML's documentation say?

Cheers

&nbsp;&nbsp;robert

> **···**
>
> On 21.10.2008 01:04, Ryan Fitz wrote:
