簡體   English   中英

通過JAXB將Bean對象編組為具有相同屬性的XML標簽

[英]Marshalling a bean object to XML tags having same attributes through JAXB

我正在使用JAXB編組Bean對象。 我搜索了很多博客,但是找不到可行的解決方案。 我也是JAXB的新手,因此任何建議將不勝感激。

所需的xml結構為:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <REPORT REPORT_YEAR="2016">
         <STANDARD_FINANCIALS>
              <BALANCE_SHEET>
                   <ASSET label = "ASSET">76</ASSET>
                   <LIABILITY label = "LIABILITY">90</LIABILITY>
              </BALANCE_SHEET>
         </STANDARD_FINANCIALS>
    </REPORT>

我正在使用的POJO(bean)類是:

@XmlRootElement(name = "REPORT")
public class Report {
    private String reportYear;
    public String getReportYear() {
        return reportYear;
    }
    @XmlAttribute(name = "REPORT_YEAR")
    public void setReportYear(String reportYear) {
        this.reportYear = reportYear;
    }
    private StandardFinancials financials;

    public StandardFinancials getFinancials() {
        return financials;
    }

    @XmlElement(name = "STANDARD_FINANCIALS")
    public void setFinancials(StandardFinancials financials) {
        this.financials = financials;
    }
    public Report(){
    }
    public Report(String reportYear, StandardFinancials financials) {
        super();
        this.reportYear = reportYear;
        this.financials = financials;
    }
}

public class StandardFinancials {
    private BalanceSheet balanceSheet;
    @XmlElement(name = "BALANCE_SHEET")
    public void setBalanceSheet(BalanceSheet balanceSheet) {
        this.balanceSheet = balanceSheet;
    }

    public StandardFinancials(BalanceSheet balanceSheet) {
        super();        
        this.balanceSheet = balanceSheet;
    }       
}

另一個是:

public class BalanceSheet {
    private String liability;
    private String asset;
    public String getLiability() {
        return liability;
    }
    @XmlElement(name = "LIABILITY")
    public void setLiability(String liability) {
        this.liability = liability;
    }
    public String getAsset() {
        return asset;
    }
    @XmlElement(name = "ASSET")
    public void setAsset(String asset) {
        this.asset = asset;
    }
    public BalanceSheet(String liability, String asset) {
        super();
        this.liability = liability;
        this.asset = asset;
    }

}

主要方法是:

public class Jaxbmarshelling {
    public static void main(String[] args) throws Exception {           
        JAXBContext context=JAXBContext.newInstance(Report.class);
        Marshaller marshaller=context.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,true);          
        BalanceSheet balanceSheet = new BalanceSheet("90", "76");           
        StandardFinancials financials = new StandardFinancials(balanceSheet);
        Report report = new Report("2016",financials);
        StringWriter xml = new StringWriter();
        marshaller.marshal(report, xml);
        System.out.println(xml.toString());         
    }    
}

但我得到的輸出xml是:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<REPORT REPORT_YEAR="2016">
    <STANDARD_FINANCIALS>
        <BALANCE_SHEET>
            <ASSET>76</ASSET>
            <LIABILITY>90</LIABILITY>
        </BALANCE_SHEET>
    </STANDARD_FINANCIALS>
</REPORT>

我無法獲取資產負債表類每個元素的標簽屬性。 請指導我獲取所需的xml。

將您的assetliability字段設為POJO,您可以使用注釋來控制它們的外觀:

public static class BalanceSheet {
    private Entry liability;
    private Entry asset;
    public Entry getLiability() {
        return liability;
    }
    @XmlElement(name = "LIABILITY")
    public void setLiability(Entry liability) {
        this.liability = liability;
    }
    public Entry getAsset() {
        return asset;
    }
    @XmlElement(name = "ASSET")
    public void setAsset(Entry asset) {
        this.asset = asset;
    }
    public BalanceSheet(Entry asset, Entry liability) {
        super();
        this.liability = liability;
        this.asset = asset;
    }
}

public static class Entry {
    private String label;
    private int amount;

    public int getAmount() {
        return amount;
    }

    @XmlValue
    public void setAmount(int amount) {
        this.amount = amount;
    }

    public String getLabel() {
        return label;
    }

    @XmlAttribute(name="label")
    public void setLabel(String label) {
        this.label = label;
    }

    public Entry() {
    }

    public Entry(String label, int amount) {
        this.label = label;
        this.amount = amount;
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM