2
<?xml version='1.0'?>
<info>
     <contract>
       <symbol>IBM</symbol>
       <sectype>STK</sectype>
       <exchange>SMART</exchange>
       <currency>USD</currency>
    </contract>
    <order>
      <action>SELL</action>
      <quantity>100</quantity>
      <ordertype>LMT</ordertype>
      <imtprice>imtprice</imtprice>
      <transmit>false</transmit>
   </order>
</info>

I want to use jaxb annotations with existing java classes to create above XML input but i don't know how create nested xml structure based on Java classes

1 Answer 1

3

Try this:

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(propOrder = {"contract", "order"})
public class Info {
@XmlElement(required = true)
private Contract contract;
@XmlElement(required = true)
private Order order; // Getters and setters
}

Another class:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(propOrder = {"symbol", "sectype", "exchange", "currency"})
public class Contract {
@XmlElement(required = true)
private String symbol;
@XmlElement(required = true)
private String sectype;
@XmlElement(required = true)
private String exchange;
@XmlElement(required = true)
private String currency;

//Getters and setters
}

Create an order class the same way.

Sign up to request clarification or add additional context in comments.

2 Comments

Un-annotated fields will be treated as @XmlElement, if you don't need to specify required=true (which corresponds to minOccurs="1" in the schema) then you don't need use as many @XmlElement annotations.
What should i write if i want 3 instance of <order> tag in xml instead of only one

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.