30 Jan 2018

Parsing the Java Object to XML and XML to Java Object using the Castor xml API

4:48 PM java 0 Comment

Here we want to describe and give the example to parse the java objects to xml and xml to java objects using the castor api with the user-defined mapping.

What is Castor XML

Castor XML is an XML data binding framework. Unlike the two main XML APIs, DOM (Document Object Model) and SAX (Simple API for XML) which deal with the structure of an XML document, Castor enables you to deal with the data defined in an XML document through an object model which represents that data.

Castor XML can marshal almost any “bean-like” Java Object to and from XML. In most cases the marshalling framework uses a set of ClassDescriptors and FieldDescriptors to describe how an Object should be marshalled and unmarshalled from XML.

For those not familiar with the terms “marshal” and “unmarshal”, it’s simply the act of converting a stream (sequence of bytes) of data to and from an Object. The act of “marshalling” consists of converting an Object to a stream, and “unmarshalling” from a stream to an Object.

Example:

Following are the Java beans to parse

Company.java

Product.java

Employee.java

→ We should need to specify the default(zero-parameterized) constructor to every bean. Otherwise it will throws the following exception.

“org.exolab.castor.mapping.MappingException: The Java class com.castor.model.Company is not constructable — it does not contain a default public constructor.”

→ We should need to specify setters and getters for fields. Otherwise it will throw the following exception.

“org.exolab.castor.mapping.MappingException: The method getId/isId in class com.castor.model.Company accepting/returning object of type int was not found”.

Following is the service to create the Company instances:

CompanyService.java

CompanyServiceImpl.java

Following is the mapping file to configure the user defined mapping, that allows the (partial) definition of a customized mapping between Java classes (and their properties) and XML.

company_mapping.xml:

→ Here <mapping> is the root tag for Castor mapping file.

<class> is the child for it and this is respect to the java bean.

<map-to> is the child for class and in which we can specify the “xml” attribute value by how we want to see the class tag name.

<field> is the child for <class> which is respect to the field in the java bean.

<bind-xml> is the child for <field>, in which we can provide the “name” attribute value with how we want to see the tag names in generated xml file.

Here we have used some of other attributes for elements, a brief description about those as follows.

ns-uri: To specify the namespace.

ns-prefix: To specify the prefix for namespace.

node: To specify how our field to be generate as attribute or as element.

type: To specify the datatype of the field.

collection: If the datatype is the collection of something we need to specify the collection type.

location: Which is used to specify an element in which we want have some of other related child elements. We can give the name for it. Generally this can be useful for collection of objects.

For more information on the Castor mapping see the Castor Reference.

All set, now we need to test. Here we are providing the test with normal java main method.

CompanyParsingTest.java

→ In this client program we have provided the code, to both marsharlling(java To XML) and UnMarshalling(XML to Java).

→ The Castor will generates the output file with the default XML declaration as

“<?xml version=”1.0″ encoding=”UTF-8″ ?>”

If we want to customize the XML declaration we need to manage in java code like as follows

writer.append(“<?xml version=\”1.0\” encoding=\”UTF-8\” standalone=\”yes\”?>”);

After appending our own XML declaration to writer, we need to suppress Castor not to generate the XML declaration.

marshaller.setSupressXMLDeclaration(true);

→ When we specify the namespace prefix to the <class>, it will generate the prefix to all the child <field> elements which are having the pre-defined data types (it will not generate to the user- defined data types).

→ If we want not to generate namespace prefix we should manage that in java code and we need to configure that in mapping file as like normal fields. Castor is not provided any mapping mechanism for this.

 

Thank You 🙂

Leave a Reply