Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

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!

like image 838
Alex Dean Avatar asked Mar 17 '26 14:03

Alex Dean


1 Answers

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:

  • http://blog.bdoughan.com/2011/05/overriding-jaxbs-name-mangling.html
like image 71
bdoughan Avatar answered Mar 19 '26 05:03

bdoughan