When my company asked me to work on this article for the client, I got a chance to work with JAXB concepts. I wanted to share my knowledge with you guys so that this will be helpful to me and people who are working on Java XML concepts.
eXtensible Markup Language (XML) is a platform and language-independent way of defining tags. XML allows you to use meaningful tags to represent data in a structured format. Unlike HyperText Markup Language (HTML), which is used to display text, XML is a language to create extensible custom tags to represent the structure of data.
SAX parser is an event driven low-level parser, that responds to the elements of an XML document as it parses through the documents. The data is not maintained in memory. Efficiency of parsing data into elements is the key feature of SAX parser. DOM parser is a high-level parser that maintains the data structures of a document in memory as it parses through the document. This helps you to manage and manipulate data as needed. DOM API follows the tree model for maintaining the data in memory.
Formerly known as Project Adelard, Java Architecture for XML binding (JAXB) combines the benefits of Document Object Model (DOM) parser and Simple API for XML (SAX) parser.
JAXB reduces the execution time significantly and creates a robust object model for enterprise level applications.
This reference point explains how to use JAXB to create and distribute high-performance XML-enabled applications with minimum effort.
Overview of XML
This table describes the important components used in XML:
|Element tags||Used to describe elements|
|Processing Instructions||Special instructions to the XML processor|
|Document Type Declarations (DTD)||Used to carry out structure bound data modeling|
|Entity references||Used to represent the aliases of an entity data|
|Comments||Used to identify and highlight the importance of a specific function/procedure in an XML document. Similar to other conventional packages, XML processors and parsers do not consider comments while compiling|
|Marked Sections||The data in an XML document, which are marked, will not be processed or parsed by XML applications, although the data is valid for parsing and processing XML documents.|
XML parsers are used to check whether XML documents are well formed or not. In addition, they are used to convert XML nodes into Java objects or elements. There are two types of parsers: validating and non-validating. Validating parsers verify that the XML document conforms to a DTD or a schema.
A Java application that uses XML technologies needs to parse the data objects of XML. The parsing is done based on the schemas and DTDs that are defined for that particular document. You can use various technologies to parse an XML document into Java objects. Some of which are SAX, DOM and JAXB.
You can use JAXB API to map XML documents to Java objects. You can use JAXB API and tools to marshal or convert XML contents to Java objects. You can access and validate the Java objects against a schema. In addition, you can use JAXB API to un-marshal or convert Java objects into an XML document.
Advantages of JAXB
JAXB provides a layer of abstraction between XML documents and Java applications. This helps you develop enterprise applications. JAXB applications can match the speed of SAX by using the binding mechanism, which maintains information in the memory.
In addition, JAXB can convert data in XML elements into equivalent valid Java objects and set constraints with the help of XML schemas or DTDs.
JAXB frees you from writing and debugging the XML parsing codes. With generated conversion codes, you can write the applications that can access the XML documents through normal Java data interfaces.
You can extend the functionalities for the generated class codes. In Java applications, you may want to have control over the XML schema components through the Java objects. For this, you can customize the binding schema for the different needs.
Because of the object orientation, JAXB based applications are helpful to access multiple XML documents.
Comparison of JAXB and SAX
The difference between JAXB and SAX are:
- SAX is an event driven model, whereas JAXB is based on binding of Java objects with XML elements.
- Data storage capability is higher in JAXB compared to SAX.
- Pre-complied Java classes and predefined schema logic enables JAXB parser to avoid dynamic interpretation, enhancing its efficiency compared to SAX parser.
Comparison of JAXB with DOM
Although both JAXB and DOM have data storage capabilities, JAXB applications are specific to a single schema, whereas DOM applications can be managed with multiple schemas. JAXB applications are faster than DOM applications.
Since JAXB applications are specific to a single schema, they can utilize memory more efficiently in comparison to DOM applications.
The basic components required for XML data binding are:
- Binding Compiler: Transforms or binds the DTDs or Schemas with Java objects.
- Runtime Binding Framework: An API used for supporting the basic operations such as marshaling, un-marshaling, and validation of Java classes.
- Marshaling: The process of generating Java objects for a given XML document.
- Un-marshaling: The process of generating XML documents from Java objects.
- Validation: The process of identifying the desired Java object based on DTD/Schema.
- Binding Language: Any language that supports XML, such as DTD, can be used as binding language. This language describes the binding of DTDs or Source Schema with a Java representation. The declarations are intended to generate Java packages, classes, or interfaces.
Java representation of JAXB architecture
Complex data contents defined by using DTD or Schema in an XML document are bound to the content interfaces, which are generated by a JAXB compiler. Simple contents such as attributes and elements of an XML node are directly bound to the properties of the content interfaces and can be accessed through setter and getter methods. Using properties, you can reference two interfaces at the same time. You can effectively use the generated classes as both content interfaces and implementation interfaces to extend the functionality, which makes JAXB architecture more flexible and competent.
Binding Schema/DTD to Java classes
Binding Framework API helps you to compile XML Schema and Java classes for marshalling and un-marshalling of Java objects.
Binding Derived Classes
When you bind DTD with a binding schema:
- Course-grained schema components, such as element-type definitions, are bound to a content class.
- Fine-grained components, such as attribute declarations, are bound directly to a property within a content class.
- A property is realized by a set of access methods.
- The methods include the standard get and set events to retrieve and modify a property value. You can also delete and re-initialize a property value.
You can use:
- Schema compiler to bind DTD with a binding schema and generate Java classes and interfaces.
- Source schema language such as DTD to write source schema.
- The binding language to write binding schema.
If you do not mention the attribute types for components in the binding schema, the schema compiler uses the default
String data type.
Validations on XML Schema and Binding Objects
You have to validate a binding object against three constraints:
- Type constraint: Imposes requirements of values that may be given as an attribute to an XML document or content of a
- Local structural constraint: Imposes requirements on every instance of a specific element type.
- Global structural constraint: Imposes requirements on the entire document.
Static Schema Constraint Enforcement
The Java programming language ensures that a schema constraint is checked at compile time and dynamically generates the available methods for the attribute mentioned in the schema. For example, if you have a schema constraint attribute by the name
String, the derived methods will be:
public void setName (String name);
public String getName ();
Simple Dynamic Schema Constraint Enforcement
The enforcement performs a run-time check and throws an exception on failure. This is best suited for type constraints that do not map directly to the Java classes or primitives.
For example, if an attribute is constrained to an integer value between 0 and 100, the
set method can possibly throw a run-time exception when the value passed to the method is out of range.
Complex Dynamic Schema Constraint Enforcement
The enforcement performs a check at run-time and throws an exception on failure.
- Local structural constraint checks whether or not the types of complex type's children match the schema's content model.
- Global structural constraint examines the entire content tree to check for uniformity of ID values and check if every
IDREF has a corresponding ID.
The steps that you need to follow before working on JAXB generated classes are:
- Install JAXB software
- Write a binding schema, which contains instructions on how to bind DTD to classes.
- Execute the schema compiler with the DTD and binding schema as input to the complier and generate the source code.
- Compile the source code to generate the classes.
Installing JAXB Software
For getting information about XML based technologies:-
I would like to share my knowledge with you guys and wanted to update this article by putting some extra information. Please help on this so that it will be helpful for me and the persons who are working on Java XML.