gpt4 book ai didi

java - 为 Jasper 报告创建 XML

转载 作者:太空宇宙 更新时间:2023-11-04 13:53:24 25 4
gpt4 key购买 nike

我使用以下 Java 代码创建 .xml 文件

public static void main(String[] args) {
try {

ArrayList<MyBean> ll = new ArrayList<>();
ll.add(new MyBean(1));
ll.add(new MyBean(2));

FileOutputStream fos = new FileOutputStream("C:\\temp\\mybean.xml");
BufferedOutputStream bos = new BufferedOutputStream(fos);
XMLEncoder xmlEncoder = new XMLEncoder(bos);
xmlEncoder.writeObject(ll);
xmlEncoder.close();

} catch (Exception e) {
}
;

}
public static class MyBean implements Serializable {
private static final long serialVersionUID = 1L;
private double fxEffectRealized;
private double changeWPRealized;

public MyBean() {

}
public MyBean(int i) {
fxEffectRealized = Math.random() * 100;
changeWPRealized = Math.random() * 100;
}

public double getFxEffectRealized() {
return fxEffectRealized;
}

public void setFxEffectRealized(double fxEffectRealized) {
this.fxEffectRealized = fxEffectRealized;
}

public double getChangeWPRealized() {
return changeWPRealized;
}

public void setChangeWPRealized(double changeWPRealized) {
this.changeWPRealized = changeWPRealized;
}


}

它创建一个 .xml 文件,如下所示

<?xml version="1.0" encoding="UTF-8"?>
<java version="1.7.0_02" class="java.beans.XMLDecoder">
<object class="java.util.ArrayList">
<void method="add">
<object class="com.ch.reports.ReportJasper$MyBean">
<void property="changeWPRealized">
<double>12.206382535941362</double>
</void>
<void property="fxEffectRealized">
<double>4.772624038704009</double>
</void>
</object>
</void>
<void method="add">
<object class="com.ch.reports.ReportJasper$MyBean">
<void property="changeWPRealized">
<double>29.554973174761056</double>
</void>
<void property="fxEffectRealized">
<double>33.64290090087834</double>
</void>
</object>
</void>

</object>
</java>

但是为了让 Jasper Reports 能够读取它,我真的需要它看起来像

<?xml version="1.0" encoding="UTF-8"?>
<jasperReport>
<MyBean>
<changeWPRealized>12.206382535941362</changeWPRealized>
<fxEffectRealized>4.772624038704009</fxEffectRealized>
</MyBean>
<MyBean>
<changeWPRealized>29.554973174761056</changeWPRealized>
<fxEffectRealized>33.64290090087834</fxEffectRealized>
</MyBean>
</jasperReport>

执行此操作的最佳方法是什么?

最佳答案

使用 @ChristofferPass 的 JAXB 建议(谢谢!)

public static void main(String[] args) {
try {
ListBeans lb = new ListBeans();
lb.add(new MyBean(1));
lb.add(new MyBean(2));

JAXBContext jaxbContext = JAXBContext.newInstance(ListBeans.class);
Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
jaxbMarshaller.marshal(lb, new File("c:\\temp\\mybean.xml"));

} catch (Exception e) {
}
;

}

@XmlRootElement(name = "ROOT")
public static class ListBeans {

private List<MyBean> listBeans = null;

public ListBeans() {

}
@XmlElement (name="BEAN")
public void setListBeans(List<MyBean> listBeans) {
this.listBeans = listBeans;
}


public void add(MyBean mb) {
if (this.listBeans == null) {
this.listBeans = new ArrayList<MyBean>();
}
this.listBeans.add(mb);
}


public static class MyBean {
private double fxEffectRealized;
private double changeWPRealized;

public MyBean() {

}
public MyBean(int i) {
fxEffectRealized = Math.random() * 100;
changeWPRealized = Math.random() * 100;
}

public double getFxEffectRealized() {
return fxEffectRealized;
}

public void setFxEffectRealized(double fxEffectRealized) {
this.fxEffectRealized = fxEffectRealized;
}

public double getChangeWPRealized() {
return changeWPRealized;
}

public void setChangeWPRealized(double changeWPRealized) {
this.changeWPRealized = changeWPRealized;
}
}
}

关于java - 为 Jasper 报告创建 XML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30077676/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com