【问题标题】:Marshalling a bean object to XML tags having same attributes through JAXB通过 JAXB 将 bean 对象编组为具有相同属性的 XML 标记
【发布时间】:2016-12-24 17:38:12
【问题描述】:

我正在使用 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。

【问题讨论】:

    标签: java xml jaxb


    【解决方案1】:

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

    【讨论】:

    • 但这不是一个通用的方法......如果我有数千个这样的属性会发生什么
    • 属性之间有什么变化?标签?
    猜你喜欢
    • 1970-01-01
    • 2021-10-08
    • 1970-01-01
    • 2012-06-27
    • 2011-03-18
    • 2018-04-22
    • 1970-01-01
    • 1970-01-01
    • 2014-08-13
    相关资源
    最近更新 更多