Issue Details (XML | Word | Printable)

Key: GROOVY-2140
Type: Improvement Improvement
Status: Closed Closed
Resolution: Fixed
Priority: Trivial Trivial
Assignee: Paul King
Reporter: Red Neck
Votes: 0
Watchers: 0
Operations

If you were logged in you would be able to see more operations.
groovy

Handle Null Attributes in MarkupBuilder

Created: 25/Sep/07 09:01 AM   Updated: 13/Jan/08 05:57 AM
Component/s: XML Processing
Affects Version/s: 1.0
Fix Version/s: 1.1-rc-1

Time Tracking:
Not Specified


 Description  « Hide
Can we alter MarkupBuilder to ignore optional attributes that aren't specified (contain null values).

Currently MarkupBuilder throws a NullPointerException if I create a node using attributes that contain a null map value. For example:

def writer = new StringWriter()
def builder = new MarkupBuilder(writer)

builder.A {
B {
C(D: 'E', F: null) {

}
}
}
// throws NullPointerException in MarkupBuilder.createNode

When building XML using MarkupBuilder, I find it useful to specify all attributes (required or optional) for a node in one line rather than having to implement ugly logic checks myself like follows:

builder.A {
B {
def map = [:]
if (E) { map['D'] = 'E' }
if (F) { map['F'] = 'G' }
C(map) {

}
}
}



 All   Comments   Work Log   Change History      Sort Order: Ascending order - Click to sort in descending order
Paul King added a comment - 29/Sep/07 03:24 AM
Should be fixed in HEAD

Paul King added a comment - 10/Oct/07 06:17 AM
No further feedback, assuming fixed

Paul King added a comment - 13/Jan/08 05:57 AM
Now that omitting attributes is configurable after GROOVY-1684 has been fixed, I have changed the default behavior of handling null attributes to be the same as StreamingMarkupBuilder. Please set omitNullAttributes to true if you don't want them to appear in your markup.