如何使用schema + JaxB中的Java类

时间:2015-02-05 01:17:45

标签: java xml jaxb

我正在构建一个由数据库,报价服务器,事务服务器组成的日间交易系统......它还需要一个记录存储在XML中的每个事务的LogServer。我们得到了一个模式,我用它来用JaxB创建类。我是新手,我真的迷失了如何正确使用它。

每个事务都有不同的类型(即QuoteServerType,UserCommandType ......等)我还认为是一个名为LogType的包装类型。

这是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的新文件,我认为应该创建一个类型为&#34; LogType&#34;的对象列表。当我创建每个对象时,我认为我应该如何声明该对象是QuoteServerType类型,但我真的不知道如何做到这一点。到目前为止,我有这么多:

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

}
}

1 个答案:

答案 0 :(得分:0)

所以你有使用JAXB从模式生成的那些类,基本上你想要一个XML String保存到数据库,对吗?

看看我的通用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对象传递给它。

希望这有帮助。