2012-05-29 5 views
2

테이블 이름, 행 개수 및 열 목록이있는 테이블 빈을 XML로 출력하려고합니다. 내가 속성처럼 주석을, 그들은 보여 그래서이 정의를 :JAXB를 사용하여 요소가 표시되지 않습니다.

@XmlRootElement(name = "table") 
public class Table { 

    private String tableName; 
    private int rowCount; 
    private List<Column> columnList; 

    @XmlAttribute(name = "name") 
    public String getTableName() { 
     return tableName; 
    } 

    @XmlAttribute(name = "rowCount") 
    public int getRowCount() { 
     return rowCount; 
    } 

    @XmlElement(name = "column") 
    public List<Column> getColumnList() { 
     return columnList; 
    } 

} 

출력이 :

<tables> 
    <table name="GGS_MARKER" rowCount="19190"> 
    <column> 
     <columnName>MARKER_TEXT</columnName> 
     <datatype>VARCHAR2</datatype> 
     <length>4000.0</length> 
    </column> 
... 

하지만 @XmlElement와 @XmlAttribute을 변경하는 경우, 그냥 보여줍니다

<tables> 
    <table> 
    <column> 
     <columnName>MARKER_TEXT</columnName> 
     <datatype>VARCHAR2</datatype> 
     <length>4000.0</length> 
    </column> 
... 

"name"및 "rowcount"를 요소로 가져 오려면 클래스에 무엇을 넣어야합니까?

+1

그들은 확실히 나타나지 않는' 후'를? –

+0

아니요, 확인했습니다. 또한 런타임에 값을 갖도록 디버깅했습니다. –

답변

0

예제에서 수행해야 할 작업은 @XmlAttribute에서 @XmlElement으로 변경해야합니다. 게시물에있는 것처럼 get 개의 메소드 만 있고 set 메소드가 아닌 경우 @XmlElement 주석을 명시 적으로 추가해야합니다 (이 경우에는 기본적으로 매핑 해제 된 모든 속성에 @XmlElement 주석이 있다고 가정됩니다) .

package forum10794522; 

import java.util.*; 
import javax.xml.bind.annotation.*; 

@XmlRootElement(name = "table") 
public class Table { 

    static Table EXAMPLE_TABLE; 
    static { 
     EXAMPLE_TABLE = new Table(); 
     EXAMPLE_TABLE.tableName = "GGS_MARKER"; 
     EXAMPLE_TABLE.rowCount = 19190; 
     List<Column> columns = new ArrayList<Column>(2); 
     columns.add(new Column()); 
     columns.add(new Column()); 
     EXAMPLE_TABLE.columnList = columns; 
    } 

    private String tableName; 
    private int rowCount; 
    private List<Column> columnList; 

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

    @XmlElement(name = "rowCount") 
    public int getRowCount() { 
     return rowCount; 
    } 

    @XmlElement(name = "column") 
    public List<Column> getColumnList() { 
     return columnList; 
    } 

} 

데모

package forum10794522; 

import javax.xml.bind.*; 

public class Demo { 

    public static void main(String[] args) throws Exception { 
     JAXBContext jc = JAXBContext.newInstance(Table.class); 

     Marshaller marshaller = jc.createMarshaller(); 
     marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); 
     marshaller.marshal(Table.EXAMPLE_TABLE, System.out); 
    } 

} 

출력

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> 
<table> 
    <column/> 
    <column/> 
    <rowCount>19190</rowCount> 
    <name>GGS_MARKER</name> 
</table> 
관련 문제