通过JAXB

时间:2016-12-24 17:38:12

标签: java xml 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>

我无法获得资产负债表类的每个元素的label属性。 请指导我获取所需的xml。

1 个答案:

答案 0 :(得分:0)

制作您的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;
    }
}