Alex Dean
Alex Dean

Reputation: 16075

How to un/marshall underscored XML to/from camelcased Java using JAXB?

Strangely I couldn't find an existing question on this. I'm simply looking for a way (hopefully a one-liner) to do a programmatic conversion from e.g. <sales_order_number> to salesOrderNumber, and back again.

In Jackson terms, I'm looking for the JAXB equivalent of:

mapper.setPropertyNamingStrategy(new PropertyNamingStrategy.LowerCaseWithUnderscoresStrategy())

Any help gratefully received!

Upvotes: 3

Views: 1442

Answers (1)

bdoughan
bdoughan

Reputation: 149017

Note: I'm the EclipseLink JAXB (MOXy) lead and a member of the JAXB (JSR-222) expert group.

Using the standard JAXB APIs you will need to use the JAXB annotations on individual properties to override the default name.

@XmlElement(name="sales_order_number")
private String salesOrderNumber;

MOXy provides am extension mechanism (XMLNameTransformer) where you can override the default name algorithm:

Upvotes: 3

Related Questions