Join David Gassner for an in-depth discussion in this video Annotating POJO classes for use with Simple, part of XML Integration with Java.
The Simple XML Serialization library is a third party open source library. And to use it, either an Android or any other environment, you'll need to download it from the website. You'll be downloading a zip file. Extract it anywhere on your hard disk. I've extracted it to my desktop. And then, open up the resulting folder, go to the Jar Sub Folder, and copy the Jar File. I'm using version 2.7.1. You should be able to use this or any other later version.
I've also included this Jar File in the course's exercise files. I'll copy the Jar File to the clipboard, then go to Eclipse. And I've opened a project named simple annotate from the Exercise Files. I'll go to the Project Libs Folder, which already has a copy of the J SON simple library, and I'll paste this library into place. And then I'll add it to the project's Build Path. And now all of its classes and annotations are available to me.
The first step in reading or creating XML with Simple, is to add annotations to the POJO classes. This is pretty much the same model as with JAXB. But the names of the annotations are a little different. And in fact, the coding is a little bit simpler. I'll start with the customer class, which represents a single customer object. Begin by adding a root annotation above the class declaration. Be sure to import it. This and all of the other annotations in the simple framework are members of the package org.simpleframework.xml.
You can use the Root annotation in it's simple form, as long as the name of your class matches the name of the Root element of the XML file. It will be changed automatically to all lower case, as it's translated to XML. But, if you want to explicitly indicate the name of the Root, you can set a property called name. It will look like this. Name equals and then the name of the element as a literal string. Next, add annotations to each of the private fields of the class.
For each field, choose between an attribute and an element. In my XML structure, the id is represented as an attribute, so I'll add an Attribute annotation. Once again, be sure to import it. For all of the other fields, I'll use an Element annotation. Again, be sure to use the right import. After I've added that one, and made sure that I've added the import, I'll copy that line of code to the Clipboard, and paste it in, above each of the fields.
Each of these values will be saved as a text node automatically in the XML file. For any value that you want to wrap inside a CDATA section, add a property to the Element annotation. It's called data. And you'll set it to a value of true. And that means you want to treat the following field as the CDATA section instead of a simple text note. And that's it, for the data element. I've added the root annotation above the class. And added an Attribute or an Element annotation above each private field.
I'll save those changes and next, I'll move to my Customers class. The customers class looks exactly the same way it did with JAXB. It has a private field, representing a list of customer objects, it's named Customers. And then, a public setter and getter. Just as I did with the customer class, I'll create a Root annotation above the class declaration. I'll be sure to import it and I'll add the name property. And in this case, I'll set it to Customers.
Next, I'll annotate the private field, the list of customers. I'll use an annotation called Element List. I'll be sure to import it and save the change. That's acceptable, but if you leave this annotation the way it is, your nesting of the XML will be too deep. You'll get a Customer's Element inside a Customer's Element. To indicate that you want this list to be the next level inside the Root element of the XML file. Add a property called inLine, set to a value of true.
And now, that will take the List of Customers and make it a child of the root. And that's all the annotations you need. So to review. The top level class which contains my list of data, has a Root annotation above the class declaration and a name property indicating the name of the equivalent XML element. And then the list which is the private field has the Element list annotation with the setting of inLine equals true. And that means that this list will be the next level inside the XML right under the Root.
The Customer Class has a Root annotation once again with the name and then each of the fields has either an Attribute or an Element annotation. And for those fields that you want to represent as CDATA sections, you add the data equals true setting. So the annotations are complete and you're now ready to read and write XML, and we'll get to those steps in the next set of movies.
- Choosing a Java-based XML API
- Reading XML as a string
- Comparing streaming and tree-based APIs
- Parsing XML with SAX
- Creating and reading XML with DOM
- Adding data to an XML document with JDOM
- Reading and writing XML with StAX
- Working with JAXB and annotated classes
- Comparing Simple XML Serialization to JAXB