Using quick to convert between Java objects and XML

Source: Internet
Author: User
Tags zip

In recent years, XML has really brought a huge impact on the programming world. However, the complexity of XML applications (complex from the outset) has not diminished much in recent years. Developers still spend weeks (if not months) learning complex XML semantics and APIs (such as SAX and DOM) to manipulate XML. However, in the past 6 months to 12 months, a new, simpler XML API (called Quick) has become increasingly popular compared with the more complex APIs.

Data binding allows you to map directly between Java objects and XML without having to deal with XML attributes and elements. In addition, it allows Java developers to use XML without taking the time to delve into XML specifications. Quick is a data-binding API that is a project that makes Java applications fit for business purposes.

Installation and Setup

Before delving into the details of using Quick, you need to download and install the project. Please visit the Quick Web site and select Download. You can then download the. zip file for the project; When I write this article, the latest available version is Quick 4.3.1, which is available through the Quick4.3.1.zip file.

Unzip the. zip file to create a Quick distribution (distribution). Listing 1 shows the directory hierarchy:

Listing 1. Quick directory Structure

Quick4
  |
  +-- JARs
  +-- BATs
  +-- Doc
  +-- dtdParserSrc
  +-- DTDs
  +-- examples
  +-- JARs
  +-- QDMLs
  +-- QJMLs
  +-- quickSrc
  +-- UTILs
  +-- utilSrc
  +-- XSLs

The two directories that developers are most concerned with are quick4/bats (which should be added to the PATH environment variable) and Quick4/jars (which contains jar files that should be added to the CLASSPATH environment variable). Specifically, you need to add Dtdparser115.jar, Quick4rt.jar, and Quick4util.jar to the current classpath. You will also need a SAX parser implementation, such as the xerces-j of the Apache project. Also, add Xerces.jar or your own favorite parser to the classpath.

Java Classes and XML documents

Data binding will focus on XML and Java, so let's look at how to relate these XML documents and Java classes to Quick. To illustrate these issues, let's look at a few simple Java classes and a simple XML document.

A simple XML document

First, listing 2 shows a small XML document. I've made things simpler, so after you've read 10 or 15 Java classes, you won't be able to understand the concepts.

Listing 2. Represents a person's XML

<?xml version="1.0"?>
<!DOCTYPE person SYSTEM "person.dtd">
<person>
  <firstName>Gary</firstName>
  <lastName>Greathouse</lastName>
  <address type="home">
   <street>10012 Townhouse Drive</street>
   <city>Waco</city>
   <state>TX</state>
   <zipCode>76713</zipCode>
  </address>
  <phoneNumber>
   <type>home</type>
   <number>2545550287</number>
  </phoneNumber>
  <phoneNumber>
   <type>work</type>
   <number>2545556127</number>
  </phoneNumber>
</person>

Although Listing 2 is not a major example of how to write XML, there are several important points about Quick that are noteworthy. You also need to study the DTD for the document shown in Listing 3.

Listing 3. Person.xml's DTD

<!ELEMENT person (firstName, lastName, address+,  phoneNumber+)>
<!ELEMENT firstName (#PCDATA)>
<!ELEMENT lastName (#PCDATA)>
<!ELEMENT address (street, city, state, zipCode)>
<!ATTLIST address
      type  (home | work | other)  "home"
>
<!ELEMENT street (#PCDATA)>
<!ELEMENT city (#PCDATA)>
<!ELEMENT state (#PCDATA)>
<!ELEMENT zipCode (#PCDATA)>
<!ELEMENT phoneNumber (type, number)>
<!ELEMENT type (#PCDATA)>
<!ELEMENT number (#PCDATA)>

Related Article

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.