# REXML and Empty-Elements

**URL:** <https://www.ruby-forum.com/t/rexml-and-empty-elements/151400>\
**Category:** Ruby\
**Created:** [October 21, 2008, 1:05am UTC](https://www.ruby-forum.com/t/rexml-and-empty-elements/151400 "2008-10-21T01:05:28Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![alphalux](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/65b543/32.png) [@alphalux](https://www.ruby-forum.com/u/alphalux)\
**Post date:** [October 21, 2008, 1:05am UTC](https://www.ruby-forum.com/t/rexml-and-empty-elements/151400/1 "2008-10-21T01:05:28Z")

</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:  
  
John  
123 Maple Lane  
  
Chicago  
IL

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:  
  
John  
123 Maple Lane  
  
Chicago  
IL

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

Thank you for your time.

Ryan

---

<div class="post-metadata">

**Author:** ![alphalux](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/65b543/32.png) [@alphalux](https://www.ruby-forum.com/u/alphalux)\
**Post date:** [October 21, 2008, 8:15am UTC](https://www.ruby-forum.com/t/rexml-and-empty-elements/151400/2 "2008-10-21T08:15:39Z")

</div>

On 21.10.2008 01:04, Ryan Fitz wrote:

> 123 Maple Lane  
>   
> Chicago  
> IL
> 
> 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.

> use empty-element tags. I would rather it be displayed as  
> . 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

robert
