为 Jasper Reports 创建 XML
Creating XML for Jasper Reports
我使用以下 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 代码创建一个 .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;
}
}
}