简体   繁体   中英

Pojo required for Converting xml(without tag value) to java object for JAXB

I want to convert the following xml to java object using JAXB.

<?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>

I have created the Colindex POJO as

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

And

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

After conversion the value of the address_type is not getting converted. How to convert this(when it does not have any tag)?

To map the value field with the JAXB element column value, you must specify @XmlValue as below.

@XmlValue
protected String value;

Complete class

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

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM