Lipe
Lipe

Reputation: 71

How to generate XML custom tags with JAXB?

My VO:

public class Address {     
    private String street;
    private String complement;

And JAXB generates the output:

<address>
   <street>Some avenue</street>
   <complement>Some number and other info</complement>
</address>

But when marshalling, I need tell JAXB: for this XML, the atribute "street" must be named "popcorn", and the atribute "complement" must be named "butter", generating the output:

<address>
   <popcorn>Some avenue</popcorn>
   <butter>Some number and other info</butter>
</address>

Please note that for now, I just have to change the atributes names, not the type name. And I must make it happen with JAXB, if possible. I made it happen with replaceAll() method, but I need a better way for it. I'm at Java 6, and I can't modify the VO's, I just must generate the XML with some custom tags.

Upvotes: 0

Views: 1262

Answers (1)

user268396
user268396

Reputation: 12006

Annotate the fields or -if using getter/setters- either getter or setter method with @XmlElement(name="some-valid-xml-tag-name") to get desired behaviour:

@XmlElement(name="popcorn")
private String street;

See also: http://docs.oracle.com/javaee/6/api/javax/xml/bind/annotation/XmlElement.html

Upvotes: 1

Related Questions