将xml(没有标记值)转换为JAXB的java对象需要Pojo

时间:2017-01-23 12:43:12

标签: java xml xml-parsing jaxb

我想使用JAXB将以下xml转换为java对象。

<?xml version="1.0" encoding="UTF-8"?>
<Colindex>
   <column name="COLNAMES">+ADDRESS_TYPE</column>
   <column name="INDNAME">ADDRESS_TYPE_PK</column>
   <column name="TABNAME">ADDRESS_TYPES</column>
   <column name="UNIQUERULE">P</column>
   <column name="MADE_UNIQUE">N</column>
   <column name="COLCOUNT">1</column>
   <column name="UNIQUE_COLCOUNT">1</column>
   <column name="INDEXTYPE">REG</column>
   <column name="COMPRESSION">N</column>
</Colindex>

我已将Colindex POJO创建为

@XmlRootElement(name = "Colindex")
public class Colindex {

    private List<Db2Column> column;

    public List<Db2Column> getColumn() {
        return column;
    }

    @XmlElement(name = "column")
    public void setColumn(List<Db2Column> column) {
        this.column = column;
    }
}

并且

public class Db2Column {

    private String name;

    private String value;

    public String getName() {
        return name;
    }

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

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }
}

转换后,不会转换address_type的值。如何转换(当它没有任何标签时)?

1 个答案:

答案 0 :(得分:0)

要将value字段映射为JAXB元素column值,您必须指定@XmlValue,如下所示。

@XmlValue
protected String value;

完整课程

@XmlAccessorType(XmlAccessType.FIELD)
public class Db2Column {
    @XmlAttribute(name = "name")
    private String name;
    @XmlValue
    private String value;

    public String getName() {
        return name;
    }

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

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }
}