Forum: Ruby REXML and Empty-Elements

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
6aa873ff6a2193fe4ab3a61b7c214764?d=identicon&s=25 Ryan Fitz (twism113)
on 2008-10-21 01:05
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>
  <Name>John</Name>
  <Address1>123 Maple Lane</Address1>
  <Address2></Address2>
  <City>Chicago</City>
  <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>
  <Name>John</Name>
  <Address1>123 Maple Lane</Address1>
  <Address2 />
  <City>Chicago</City>
  <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
E0d864d9677f3c1482a20152b7cac0e2?d=identicon&s=25 Robert Klemme (Guest)
on 2008-10-21 08:15
(Received via mailing list)
On 21.10.2008 01:04, Ryan Fitz wrote:
>   <Address1>123 Maple Lane</Address1>
>   <Address2></Address2>
>   <City>Chicago</City>
>   <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.

> 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

  robert
This topic is locked and can not be replied to.