2015-02-05 44 views
0

我正在构建一个由数据库,报价服务器,交易服务器组成的日内交易系统......它还需要一个记录每个存储在XML中的交易的LogServer。我们得到了一个模式,并用它来创建JaxB的类。我对此很陌生,对于如何正确使用它我真的很失落。如何从模式+ JaxB使用Java类

每个事务都有一个不同的类型(即QuoteServerType,UserCommandType ...等等)也有我认为是称为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; 
} 

}

下面是类型之一的类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; 
} 

}

这里是ObjectFactory类

@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); 
} 
} 

我创建了一个名为LogServer.java的新文件,我认为它应该创建一个类型为“LogType”的对象列表。当我创建每个对象时,我认为我应该如何声明该对象是QuoteServerType类型的,但我对如何做到这一点真的很感慨。所以,你必须使用JAXB从模式生成的类

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(); 
    } 

} 
} 

回答

0

,基本上你想有保存到数据库中的XML字符串,右:到目前为止,我有这么多?

看看我的通用marshalObject方法:

/** 
    * 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(); 
    } 

你基本上只是调用该方法,并通过您的QuoteServerType反对。

希望这会有所帮助。

+0

是的,这是真的,但我真正挣扎的是包装。我们必须能够记录不同类型的事务,以便文件LogType包含元素列表....我认为这就是它的目的。基本上,我们必须创建一个对象列表,这些对象可以是文件LogType中定义的不同类型的对象......我只是不知道该怎么做。谢谢 – nhoughto

+0

嗯..尝试将包含具有不同元素的List的'LogType'对象传递给方法。如果JAXB生成了这些注释,那应该有效。 – jmcg