Jaxb unmarshall返回空值

时间:2016-11-18 09:18:31

标签: java xml spring jaxb

您好我是 Jaxb 的新手,并尝试通过jaxb解组xml文件。我已正确添加了xmppropordername字段。

但是,我的两个XML字段填充为null,而其他字段填充完美。

我的xml文件是: -

<tables>
    <table id="1">
        <name>test1</name>
        <schema>validator_test</schema>
        <rowCountToValidate>7</rowCountToValidate>
        <columnTypeCheckRequired>FALSE</columnTypeCheckRequired>
        <additionalColumns>column1,column2</additionalColumns>
        <targetName>target1</targetName>>
    </table>
    <table id="2">
        <schema>validator_test</schema>
        <name>validate_external1</name>
        <rowCountToValidate>1</rowCountToValidate>
        <columnTypeCheckRequired>FALSE</columnTypeCheckRequired>
        <additionalColumns>column1,column2</additionalColumns>
    </table>
</tables>

我解组元素的代码是:

import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;

import org.apache.commons.lang.builder.ToStringBuilder;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

@XmlRootElement(name = "table")
@XmlType(propOrder = { "name", "schema","rowCountToValidate","columnTypeCheckRequired","additionalColumns","targetName"})
@Service
public class TableInfo {

private static final boolean TRUE = true;
@Value("${default.row.count.to.validate}")
private Integer defaultRowCountToValidate;

/**
 *
 */
public TableInfo() {
}

private String name;
private String schema;
private Integer rowCountToValidate;
private String targetName;
private String columnTypeCheckRequired;
private String additionalColumns;

@XmlElement(name = "name")
public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

@XmlElement(name = "schema")
public String getSchema() {
    return schema;
}

public void setSchema(String schema) {
    this.schema = schema;
}

@XmlElement(name = "rowCountToValidate")
public Integer getRowCountToValidate() {
    return rowCountToValidate;
}

@XmlElement(name = "columnTypeCheckRequired")
public String getcolumnTypeCheckRequired(){
    return columnTypeCheckRequired;
}

@XmlElement(name = "additionalColumns")
public String getadditionalColumns(){
    return additionalColumns;
}



public void setRowCountToValidate(Integer rowCountToValidate) {
    // If user configured value is not null and greater than zero then set
    // the value otherwise use default value
    if ((null != rowCountToValidate) && (rowCountToValidate.intValue() > 0)) {
        this.rowCountToValidate = rowCountToValidate;
    }else {
        this.rowCountToValidate = defaultRowCountToValidate;
    }
}

@XmlElement(name = "targetName")
public String getTargetName() {
    return targetName;
}

public void setTargetName(String targetName) {
    this.targetName = targetName;
}



public void setColumnTypeCheckRequired(String columnTypeCheckRequired) {
    this.columnTypeCheckRequired = columnTypeCheckRequired;
}

public void setAdditionalColumns(String additionalColumns) {
    this.additionalColumns = additionalColumns;
}

@Override
public String toString() {
    return ToStringBuilder.reflectionToString(this);
}}

我在columnTypeCheckRequired和additionalColumns中获取空值。但是其他字段正确填充。我需要设置其他字段或设置吗?

我是Jaxb的新手!有人可以帮我理解错误吗?

1 个答案:

答案 0 :(得分:2)

您必须使用驼峰式样式更改以下getter。 C和A案例。

  @XmlElement(name = "columnTypeCheckRequired")
  public String getColumnTypeCheckRequired() {
    return columnTypeCheckRequired;
  }

  @XmlElement(name = "additionalColumns")
  public String getAdditionalColumns() {
    return additionalColumns;
  }