This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Web Services and the fly likes Empty tags in Java DOM API Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Web Services
Bookmark "Empty tags in Java DOM API" Watch "Empty tags in Java DOM API" New topic
Author

Empty tags in Java DOM API

amit sanghai
Ranch Hand

Joined: Dec 05, 2000
Posts: 231
Hi,
Is there any method in Java DOM API to handle empty tags? E.g. consider the following XML file.
<?xml version="1.0"?>
<MESSAGE>
<RATES>
</RATES>
</MESSAGE>
here the output, using Java and DOM, is
parent tag is MESSAGE
root is MESSAGE
root is RATES
Value in root is null
Now, consider the following XML file:
<?xml version="1.0"?>
<MESSAGE>
<RATES></RATES>
</MESSAGE>
here the output, using Java and DOM, is
parent tag is MESSAGE
root is MESSAGE
root is RATES.
So, how do we know that there is an empty tag in the XML file?
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12769
    
    5
It isn't empty, there is a Text element in there - of course, this element has nothing but a crlf, but in strict DOM it counts as an element. As I recall, you can get rid of the Text elements by calling the normalize() method on the <MESSAGE> node. This method is part of the org.w3c.dom.Node class.
Bill
 
wood burning stoves
 
subject: Empty tags in Java DOM API