I am expecting a XML generated by JAXB which has the following pattern:
<payload>
<parameters>
<paramName>clientAssocIds</paramName>
<paramVal>0207</paramVal>
<paramName>quoteType</paramName>
<paramVal>NTB</paramVal>
<paramName>quoteDateLimitDays</paramName>
<paramVal>365</paramVal>
<paramName>externalIndicator</paramName>
<paramVal>1</paramVal>
</parameters>
</payload>
The <paramName> and <paramVal> are name and value pairs, which means <paramVal> has to follow <paramName>.
I wrote a XML Schema and use JAXB to generate the java class based on this schema, then I set all the values and marshalled the class, it didn’t generate the xml pattern I expected above.
Here is my schema:
<xsd:complexType name="sgrpCommonMessage">
<xsd:sequence>
<xsd:element name="payload" type="payload" />
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="payload">
<xsd:sequence>
<xsd:choice minOccurs="0" maxOccurs="1">
<xsd:element name="parameters" type="parameter" />
</xsd:choice>
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="parameter">
<xsd:sequence>
<xsd:element minOccurs="0" name="paramName" type="xsd:string" />
<xsd:element minOccurs="0" name="paramVal" type="xsd:string" />
</xsd:sequence>
</xsd:complexType>
And the generated xml has a few <parameters>, which is not what I expect. I want to wrap all the <paramName> and <paramVal> pairs in one <parameters>:
<payload>
<parameters>
<paramName>quoteDateLimitDays</paramName>
<paramVal>NTB</paramVal>
</parameters>
<parameters>
<paramName>clientAssocIds</paramName>
<paramVal>0207</paramVal>
</parameters>
<parameters>
<paramName>quoteType</paramName>
<paramVal>NTB</paramVal>
</parameters>
<parameters>
<paramName>externalIndicator</paramName>
<paramVal>NTB</paramVal>
</parameters>
</payload>
I am wondering if JAXB is capable of realizing such XML structure, if not, what API should I use, if yes, how?
You could have an XML schema like the following:
Then with the generated model you could do:
Which would give you the following output: