MarkupBuilder - Generate attributes without encapsulation

40 views Asked by At

Is it possible to generate attributes using MarkupBuilder() without encapsulating it. I have the following:


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

// generate strings with double quotes
xml.setDoubleQuotes(true);


invoiceId = "123456XXX";
date = "2023-01-06";
amount = 220.36;
  
xml.consolidate(amount:amount.toDouble(),
                    date: date,
                    invoiceId: invoiceId,
                    currency:'EUR');
  
}

but it generates:

<consolidate amount="220.36" date="2023-01-06" invoiceId="123456XXX" currency="EUR" />
</consolidate>

when the following is expected:

 <consolidate amount=220.36 date="2023-01-06" invoiceId="123456XXX" currency="EUR" />
</consolidate>
1

There are 1 answers

1
Andrej Istomin On

The result that you put as "expected" is not a valid XML (you may check it with the validator). The attribute's value is always quoted:

Attribute values must always be quoted. Either single or double quotes can be used.

See more here.

For that reason you shouldn't and probably can't make MarkupBuilder to produce the desired result.