XML and Java Technologies: multiple uses of data binding

Source: Internet
Author: User
Tags format object data structures interface object model variables requires
xml| data
in addition to using XML as a simple data format, data binding is one of the most popular uses of XML. Even the novice programmer can use XML in a native programming language, and in most cases does not need any XML expertise at all. Instead of introducing the solution, Brett introduces some of the discussion topics that encourage you to think about using XML and data binding. Welcome to share your thoughts with others in the XML and Java technology discussion forums.

XML for business rather than technology

With the increasing popularity of XML, more and more people pay more attention to usability. In other words, programmers and managers do not want to think of XML as a technology, have their own semantic and lexical structure, and feel that it is more like pure data, access without worrying about the details of the XML data format.

The simplest way to complete the transformation of XML from technology to business format is data binding, which remains to be argued. Data binding is the use of the Application programming interface (application programming Interface,api) to manipulate the data in an XML document so that the programmer does not have to know too much about XML, do not have to use corner brackets, or consider CDATA Partial or entity references, and so on. But even with data binding, you'll find that there are a number of options and important issues that need to be considered carefully before proceeding.

For the purposes of this article, I'll cover two basic issues related to data binding:

    1. A method of representing data through the data binding API.
    2. The use of data when it is treated as business data.

In the most common case, data binding is the transformation of data in an XML document into an object in the programming language being used.

For example, look at the following XML code:

<person> <firstName>Brett</firstName> <lastName>McLaughlin</lastName> <email> Brett@newinstance.com</email></person>

We can turn this code into objects, for example in Java™ code, which is an Person instance of a class that has member variables firstName , lastName and email . The instance should contain the data in the code snippet, and it can be accessed by means of a method call, such as a myPerson.getFirstName() method.

Although this is the most common method of data binding, an API that uses an XML document and represents an entire document as an object is also a form of data binding. These APIs include the Document Object model (Doc object model,dom), JDOM, and dom4j, all of which are used to create an object model of an XML document in Java encoding.

In these models, we use rootElement.getChild("firstName").getValue() such calls (or similar calls, depending on the details of the API). While this does require some basic knowledge of XML (understanding what elements are and the basic structure of the document), the programmer abstracts the details of parsing. This is the nature of data binding: the ability to pay more attention to data than the format of data display.

Once a more common data-binding solution, such as Sun's JAXB, is adopted, there will be less underlying XML syntax to be aware of. You can really completely use Java (or your preferred programming language) objects, methods, and variables. Even the details of the element and the document structure are hidden beneath the object created by the data-binding process.

However, the key here is (often not considered) the need to match XML data structures to objects in the system, or to create objects in the system that match the XML data format used. Both of these situations are less obvious than the mapping of XML, but it is still part of the process.

I've outlined two basic approaches here, but the two approaches are not as different as they seem at first glance. When using APIs such as DOM or JDOM, it is necessary to constantly process the structure of the document, whether it is loading XML or accessing data. In the second approach, using APIs such as JAXB, you need to process XML, build an object model that uses XML (or sometimes use tools to create the classes and objects you need). Then, at run time, the data is used more as business data, so you don't have to think about XML.

The first method would be a good choice if the XML is not a very readable format, or if it is not separated for business purposes, or if the format changes frequently. This approach requires a little more XML knowledge and the ability to use APIs (more technology-centric rather than business-centric).

On the other hand, if XML is organized according to business requirements, and the XML structure is rarely changed, you can create classes and objects at once, and then use the data as business data at run time without worrying about the underlying XML at all.


Developers often neglect to consider how to use data from an XML document based on a data-binding solution that you choose. However, this may be one of the most important factors for correctly determining the method of data binding.

Data-binding methods that convert XML data to object instances apply only to data that is used more than once. Getting the data in an XML document and converting it to member variable data in multiple objects involves a lot of processing work. To profit from this method, you need to use the data more than once.

Take a closer look at the number of times a piece of data is accessed, and consider how much data is being used. For example, suppose that 20 data is stored for each person in the XML, but only one of them is accessed in the application, but only one of them is used to transform the data with a large amount of resources. No matter how it is calculated, it is impossible to profit.

Hiding storage media is another important reason to use an object-based approach. Therefore, there may be a component in the application Person that performs some special processing on the object. You can read people data from a database or an XML document or a property file, and then convert the data to Person objects and then pass the objects to the processing component.

Even if you are only using data temporarily, this is a reasonable case for using objects to represent data. In this example, if you represent data as an object format, and other parts of your application know how to use that format, you can benefit. It also avoids the data transformation and data loading of the component, and you just want to use some type of object to do the right thing, which is a good way to implement relational isolation in your application, and it's an important design principle that you should follow (each component in your application implements only one feature and does it well).

If you don't have data to reuse, and you don't pass data to another component in your application as an object, consider using APIs such as DOM or JDOM. This is more than the amount of resources used to convert XML to a non-document format, which can benefit overall. This approach is much better than converting data to a business-specific object at a high cost, and then only using that data one or two times later.

Although the subject of this article is data binding, it is worth mentioning that in these cases even the use of a SAX (Simple API for XML) API, which does not provide the object model (in document or object format) at all, can be considered. Use it to process XML with little memory and time, and if you really need to use only one piece of data one to two times, this approach can benefit you enormously. Using APIs such as SAX requires more knowledge of XML, but it is worthwhile to understand that knowledge.

This series of articles on XML and Java technology is not a solution to the problem, but rather a way for readers to think about how to use a particular technique and api--This example discusses XML and data binding. You may agree with some of these points, but disagree with other points of view. But it should be clear that you need to think more deeply about how to use XML in your own application.

This article is intended to provide a starting point for readers. Welcome to the XML and Java technology forums, where these discussions will continue in a more interactive form. How do you use data binding, which API do you like best, or do you come up with some creative uses of data binding technology in your application? Please share this information with me ... I look forward to communicating with you on the Internet




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.