I'm using EclipseLink MOXy as my JAXB (JSR-222) provider and need some help with my mapping file to marshal my classes into XML.
I'm using an external file for my mapping.
I have two types of transactions: A and B. Both contain a header object (same object) with two fields (text1 and text2).
When marshalling these into XML, I would like the xml tag for the fields of the header of transactionA to become <headerA1> and <headerA2>, and for the ones linked to transactionB to become <headerB1> and <headerB2>.
Any idea how I could accomplish that (preferably without using inheritance)?
Here is the code:
HEADER Class
public class Header {
private String text1;
private String text2;
public Header(){}
public String getText1() {
return text1;
}
public void setText1(String text1) {
this.text1 = text1;
}
public String getText2() {
return text2;
}
public void setText2(String text2) {
this.text2 = text2;
}
}
TRANSACTION A
public class TransactionA {
private Header statementHeader;
private BigDecimal units;
private BigDecimal price;
public TransactionA(){}
public BigDecimal getUnits() {
return units;
}
public void setUnits(BigDecimal units) {
this.units = units;
}
public BigDecimal getPrice() {
return price;
}
public void setPrice(BigDecimal price) {
this.price = price;
}
public Header getStatementHeader() {
return statementHeader;
}
public void setStatementHeader(Header statementHeader) {
this.statementHeader = statementHeader;
}
}
TRANSACTION B
public class TransactionB {
private Header statementHeader;
private BigDecimal units;
private BigDecimal price;
public TransactionB(){}
public BigDecimal getUnits() {
return units;
}
public void setUnits(BigDecimal units) {
this.units = units;
}
public BigDecimal getPrice() {
return price;
}
public void setPrice(BigDecimal price) {
this.price = price;
}
public Header getStatementHeader() {
return statementHeader;
}
public void setStatementHeader(Header statementHeader) {
this.statementHeader = statementHeader;
}
}
MAPPING FILE
<java-types>
<java-type name="Statement" xml-accessor-type="NONE">
<java-attributes>
<xml-element java-attribute="tranA" />
<xml-element java-attribute="tranB" />
</java-attributes>
</java-type>
<java-type name="Header" xml-accessor-type="NONE">
<java-attributes>
<xml-element java-attribute="text1" name="headerA1" />
<xml-element java-attribute="text2" name="headerA2" />
</java-attributes>
</java-type>
<java-type name="TransactionA" xml-accessor-type="NONE">
<java-attributes>
<xml-element java-attribute="statementHeader" name="headerA" />
<xml-element java-attribute="units" />
<xml-element java-attribute="price"/>
</java-attributes>
</java-type>
<java-type name="TransactionB" xml-accessor-type="NONE">
<java-attributes>
<xml-element java-attribute="statementHeader" name="headerB" />
<xml-element java-attribute="units" />
<xml-element java-attribute="price"/>
</java-attributes>
</java-type>
</java-types>
RESULT As you can see, the tags for header B are the same as the ones for header A.
<?xml version="1.0" encoding="UTF-8"?>
<tranA>
<headerA>
<headerA1>Description</headerA1>
<headerA2>Units</headerA2>
</headerA>
<units>10</units>
<price>99999999.98999999463558197021484375</price>
</tranA><tranB>
<headerB>
<headerA1>Bheader1</headerA1>
<headerA2>Bheader2</headerA2>
</headerB>
<units>10</units>
<price>99999999.98999999463558197021484375</price>
</tranB>