REXML and Empty-Elements

R

Ryan Fitz

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
 
R

Robert Klemme

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

Better use the block form of File.open here.
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?

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
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,071
Latest member
MetabolicSolutionsKeto

Latest Threads

Top