What is simple?
Simple is a Java framework for simplifying the process of serializing and deserializing XML. With simplicity, developers can simplify the process of converting simple old Java objects (POJO) into XML documents-the so-called serialization (serialization) process. Simple also facilitates the reverse process: developers can convert XML documents into pojo-, the so-called deserialization (deserialization) process.
Simple is true, and it uses annotations to support serialization and deserialization processes. Annotate the POJO, depending on how the corresponding XML document should appear. Some of the fields are annotated as attributes and others are annotated as elements. Classes are usually annotated as root elements. During serialization, the framework handles lengthy explanations of annotations and the process of generating corresponding XML documents. Without sufficient singularity, annotations are also interpreted during deserialization of XML documents that are converted into POJO.
There are several advantages to using simple. First, it facilitates rapid application development. Simplicity is so simple that it enables developers to quickly implement robust applications that use XML serialization and deserialization without the need to complete long learning curves and heavy development work.
Second, simple does not need to be configured. As mentioned earlier, simple uses annotations. These annotations supersede the xml-based configuration file (which is typically the case with other frameworks).
Finally, simple simply lets the application that uses it add a small amount of memory footprint. The Java archive (Java Archive,jar) file is only 239 KB. Simple also does not depend on a series of other JAR files, whereas other frameworks are usually not.
Get simple
You must first download simple before you can use it. But there is both good news and bad news about the process. The good news is that the archive file is free. The bad news is that the archive file is. tar.gz format. So, if you want to use the microsoft®windows® native archive to unpack the program to open this archive, it will be disappointed. Requires the use of WINZIP or other similar archiving tools.
After extracting the file, note that there is a jar file (Simple-xml-2.1.4.jar) in the Jar directory. This JAR file is required in the classpath at compile-time and runtime.
Serialization of
Serializing an object into an XML document is a fairly straightforward process. involves two steps:
Create a POJO with the appropriate annotations.
Write a few lines of code to actually perform the serialization process.
For the purposes of this article, let's review the familiar bait theme (which is only familiar to readers who have read my article). So, listing 1 is a POJO that represents the bait information.
Listing 1. Lure class
@Root
public class Lure {
@Attribute
Private String type;
@Element
Private String Company;
@Element
Private int quantityinstock;
@Element
Private String model;
Public String GetType () {
return type;
public void SetType (String type) {
This.type = type;
Public String getcompany () {
Return company;
public void Setcompany (String company) {
This.company = Company;
Public int getquantityinstock () {
return quantityinstock
public void setquantityinstock (int quantityinstock) {
This.quantityinstock = Quantityinstock;
Public String Getmodel () {
return model;
public void Setmodel (String model) {
This.model = model;
}
}
There's really nothing complicated about this POJO. The only part of it that may initially look unfamiliar is the annotation. Again, this is intentional. Recall that the purpose of the simple framework is to realize the moral of its name.
@Root annotations Describe the root element of an XML document. Because each XML document requires a root element, be sure to include this element.
The @Attribute note above the Type field recognizes the field as a property. This property is added as a property to the root element.
The remaining annotations are @Element annotations. These annotations are located directly above the following 3 fields: Company, Quantityinstock, and model. These fields represent the elements in an XML document.
According to the JavaBean Standard, the remainder of the POJO consists of the accessor (accessor) method and the modifier (mutator) method.
Now that the POJO has been created, it's time to write the serialization code. See the code in Listing 2.
Listing 2. Lureexample class
public static void main(String[] args) {
try {
Serializer serializer = new Persister();
Lure lure = new Lure();
lure.setCompany("Donzai");
lure.setModel("Marlin Buster");
lure.setQuantityInStock(23);
lure.setType("Trolling");
File result = new File("lure.xml");
serializer.write(lure, result);
} catch (Exception e) {
e.printStackTrace();
}
}