I am building a day-trading system comprised of a database, quote-server, transaction server... it also requires a LogServer that records every transaction stored in XML. We were given a schema and I used it to create classes with JaxB. I am new to this and am really lost as to how to use it properly.
Each transaction has a different type (ie QuoteServerType, UserCommandType...etc) There is also what I believe to be wrapper type called LogType.
Here is the class for LogType
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "LogType", propOrder = {
"userCommandOrQuoteServerOrAccountTransaction"
})
public class LogType {
@XmlElements({
@XmlElement(name = "userCommand", type = UserCommandType.class),
@XmlElement(name = "quoteServer", type = QuoteServerType.class),
@XmlElement(name = "accountTransaction", type = AccountTransactionType.class),
@XmlElement(name = "systemEvent", type = SystemEventType.class),
@XmlElement(name = "errorEvent", type = ErrorEventType.class),
@XmlElement(name = "debugEvent", type = DebugType.class)
})
protected List<Object> userCommandOrQuoteServerOrAccountTransaction;
/**
* Gets the value of the userCommandOrQuoteServerOrAccountTransaction property.
*
* <p>
* This accessor method returns a reference to the live list,
* not a snapshot. Therefore any modification you make to the
* returned list will be present inside the JAXB object.
* This is why there is not a <CODE>set</CODE> method for the userCommandOrQuoteServerOrAccountTransaction property.
*
* <p>
* For example, to add a new item, do as follows:
* <pre>
* getUserCommandOrQuoteServerOrAccountTransaction().add(newItem);
* </pre>
*
*
* <p>
* Objects of the following type(s) are allowed in the list
* {@link UserCommandType }
* {@link QuoteServerType }
* {@link AccountTransactionType }
* {@link SystemEventType }
* {@link ErrorEventType }
* {@link DebugType }
*
*
*/
public List<Object> getUserCommandOrQuoteServerOrAccountTransaction() {
if (userCommandOrQuoteServerOrAccountTransaction == null) {
userCommandOrQuoteServerOrAccountTransaction = new ArrayList<Object>();
}
return this.userCommandOrQuoteServerOrAccountTransaction;
}
}
Here is the class for one of the types QuoteServerType
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "QuoteServerType", propOrder = {
})
public class QuoteServerType {
protected long timestamp;
@XmlElement(required = true)
protected String server;
@XmlElement(required = true)
@XmlSchemaType(name = "positiveInteger")
protected BigInteger transactionNum;
@XmlElement(required = true)
protected BigDecimal price;
@XmlElement(required = true)
protected String stockSymbol;
@XmlElement(required = true)
protected String username;
@XmlElement(required = true)
protected BigInteger quoteServerTime;
@XmlElement(required = true)
protected String cryptokey;
/**
* Gets the value of the timestamp property.
*
*/
public long getTimestamp() {
return timestamp;
}
/**
* Sets the value of the timestamp property.
*
*/
public void setTimestamp(long value) {
this.timestamp = value;
}
/**
* Gets the value of the server property.
*
* @return
* possible object is
* {@link String }
*
*/
public String getServer() {
return server;
}
/**
* Sets the value of the server property.
*
* @param value
* allowed object is
* {@link String }
*
*/
public void setServer(String value) {
this.server = value;
}
/**
* Gets the value of the transactionNum property.
*
* @return
* possible object is
* {@link BigInteger }
*
*/
public BigInteger getTransactionNum() {
return transactionNum;
}
/**
* Sets the value of the transactionNum property.
*
* @param value
* allowed object is
* {@link BigInteger }
*
*/
public void setTransactionNum(BigInteger value) {
this.transactionNum = value;
}
/**
* Gets the value of the price property.
*
* @return
* possible object is
* {@link BigDecimal }
*
*/
public BigDecimal getPrice() {
return price;
}
/**
* Sets the value of the price property.
*
* @param value
* allowed object is
* {@link BigDecimal }
*
*/
public void setPrice(BigDecimal value) {
this.price = value;
}
/**
* Gets the value of the stockSymbol property.
*
* @return
* possible object is
* {@link String }
*
*/
public String getStockSymbol() {
return stockSymbol;
}
/**
* Sets the value of the stockSymbol property.
*
* @param value
* allowed object is
* {@link String }
*
*/
public void setStockSymbol(String value) {
this.stockSymbol = value;
}
/**
* Gets the value of the username property.
*
* @return
* possible object is
* {@link String }
*
*/
public String getUsername() {
return username;
}
/**
* Sets the value of the username property.
*
* @param value
* allowed object is
* {@link String }
*
*/
public void setUsername(String value) {
this.username = value;
}
/**
* Gets the value of the quoteServerTime property.
*
* @return
* possible object is
* {@link BigInteger }
*
*/
public BigInteger getQuoteServerTime() {
return quoteServerTime;
}
/**
* Sets the value of the quoteServerTime property.
*
* @param value
* allowed object is
* {@link BigInteger }
*
*/
public void setQuoteServerTime(BigInteger value) {
this.quoteServerTime = value;
}
/**
* Gets the value of the cryptokey property.
*
* @return
* possible object is
* {@link String }
*
*/
public String getCryptokey() {
return cryptokey;
}
/**
* Sets the value of the cryptokey property.
*
* @param value
* allowed object is
* {@link String }
*
*/
public void setCryptokey(String value) {
this.cryptokey = value;
}
}
Here is the ObjectFactory Class
@XmlRegistry
public class ObjectFactory {
private final static QName _Log_QNAME = new QName("", "log");
/**
* Create a new ObjectFactory that can be used to create new instances of schema derived classes for package: LogServer
*
*/
public ObjectFactory() {
}
/**
* Create an instance of {@link LogType }
*
*/
public LogType createLogType() {
return new LogType();
}
/**
* Create an instance of {@link DebugType }
*
*/
public DebugType createDebugType() {
return new DebugType();
}
/**
* Create an instance of {@link AccountTransactionType }
*
*/
public AccountTransactionType createAccountTransactionType() {
return new AccountTransactionType();
}
/**
* Create an instance of {@link UserCommandType }
*
*/
public UserCommandType createUserCommandType() {
return new UserCommandType();
}
/**
* Create an instance of {@link QuoteServerType }
*
*/
public QuoteServerType createQuoteServerType() {
return new QuoteServerType();
}
/**
* Create an instance of {@link ErrorEventType }
*
*/
public ErrorEventType createErrorEventType() {
return new ErrorEventType();
}
/**
* Create an instance of {@link SystemEventType }
*
*/
public SystemEventType createSystemEventType() {
return new SystemEventType();
}
/**
* Create an instance of {@link JAXBElement }{@code <}{@link LogType }{@code >}}
*
*/
@XmlElementDecl(namespace = "", name = "log")
public JAXBElement<LogType> createLog(LogType value) {
return new JAXBElement<LogType>(_Log_QNAME, LogType.class, null, value);
}
}
I have created a new file called LogServer.java that I think should create a list of objects of type "LogType". When I create each object I think I am supposed to some how declare that that object is of type QuoteServerType but I am really at a loss as to how to do that. So far I have this much:
public static void create_Log(){
LogType newLog = new LogType();
ObjectFactory factory = new ObjectFactory();
LogType quoteCall = factory.createLogType();
try{
JAXBContext jc = JAXBContext.newInstance("LogFileTypeFiles");
Marshaller marshaller = jc.createMarshaller();
File XMLfile = new File("LogFile.xml");
marshaller.marshal(newLog,System.out);
}
catch (JAXBExcpetion e){
e.printStackTrace();
}
}
}
So you have those classes generated from a schema using JAXB and basically you want to have an XML String to be saved to a database, right?
Take a look at my generic marshalObject
method:
/**
* Marshals a JAXB Object to XML
*
* @param obj
* @returns XML in String format
* @throws MyCustomException
*/
public <T> String marshalObject(T obj) throws MyCustomException {
StringWriter stringWriter = new StringWriter();
try {
JAXBContext context = JAXBContext.newInstance(obj.getClass());
Marshaller marshaller = context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(obj, stringWriter);
} catch (JAXBException e) {
LOGGER.error("Error while marshalling Object. Reason: " + e.getMessage(), e);
throw new MyCustomException(
"Error while marshalling Object. Reason: " + e.getMessage(), e);
}
return stringWriter.toString();
}
You just basically call the method and pass your QuoteServerType
object to it.
Hope this helps.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.