package org.kodejava.bean;
import java.beans.XMLEncoder;
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.FileNotFoundException;
public class BeanToXML {
private Long id;
private String itemName;
private String itemColour;
private Integer itemQuantities;
public static void main(String[] args) {
BeanToXML bean = new BeanToXML();
bean.setId(1L);
bean.setItemName("T-Shirt");
bean.setItemColour("Dark Red");
bean.setItemQuantities(100);
try {
XMLEncoder encoder = new XMLEncoder(new BufferedOutputStream(
new FileOutputStream("Bean.xml")));
// Write an XML representation of the specified object to the output.
encoder.writeObject(bean);
encoder.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getItemName() {
return itemName;
}
public void setItemName(String itemName) {
this.itemName = itemName;
}
public String getItemColour() {
return itemColour;
}
public void setItemColour(String itemColour) {
this.itemColour = itemColour;
}
public Integer getItemQuantities() {
return itemQuantities;
}
public void setItemQuantities(Integer itemQuantities) {
this.itemQuantities = itemQuantities;
}
}
The XML persistence will be like:
<?xml version="1.0" encoding="UTF-8"?>
<java version="17" class="java.beans.XMLDecoder">
<object class="org.kodejava.bean.BeanToXML">
<void property="id">
<long>1</long>
</void>
<void property="itemColour">
<string>Dark Red</string>
</void>
<void property="itemName">
<string>T-Shirt</string>
</void>
<void property="itemQuantities">
<int>100</int>
</void>
</object>
</java>
Latest posts by Wayan (see all)
- How do I get number of each day for a certain month in Java? - September 8, 2024
- How do I get operating system process information using ProcessHandle? - July 22, 2024
- How do I sum a BigDecimal property of a list of objects using Java Stream API? - July 22, 2024