Data binding

xiaoxiao2021-03-06  49

Data Binding This noun is no longer unfamiliar to most programmers; Java language (object-oriented) has proven to be a language that is very suitable for data binding. Let's take a look at the other end of the data binding, usually there is a text file, an XML file, and a database. Most of the programmers mentioned by the article is the relationship between XML documents and Java program structures. In fact, this is just a specific application of both ends.

Several important concepts:

Marshalling is the process of transforming data in memory to a storage medium. Therefore, in the Java and XML environments, the group is to translate some Java objects into one (or more) XML documents. In the database environment, the data represented by Java is stored in the database. Obviously, the secret of group is to convert the object-oriented structure in the Java instance into the flat structure of XML, or the relationship between RDBMS (using Java technology to convert to OODBMS is actually very simple).

Unmarshalling is the process of converting data from storage to memory - just opposite the group. Therefore, you need to group XML documents into Java VM. The complexity here is not in flat data because this is not required, but is mapping from the correct data to the correct Java code variable. If the mapping is wrong, it is impossible to access the data correctly. Of course, if you try to re-group, you will also cause a bigger problem, and the problem spread is very fast.

Round-Tripping may be the most important data binding terminology that is also the most easily misunderstood. Roundband is used to describe a complete loop from the storage medium to memory and then returns to the storage medium. In the XML and Java technology environments, this means from the XML document to the Java instance variable, and then return to the XML document. Correct round-trip requirements, if there is no modification data in the middle, XML input and XML output should be equivalent.

Several data binding frameworks:

Castor frame:

Castor XML data bindings are easy to get started, and even do not need to define the XML document format. As long as your data is indicated by class JavaBean, Castor can automatically generate document formats representing these data, then reconstruct the original data from the document.

Use an example (CASTOR) to explain: JBuilder debugging

This Bean Was Auto Generated from A Schema

Listing 1. XMLEMPLOYETYPE information bean

Package com.borland.samples.xml.databinding.castor;

Import java.io.ioException;

Import Java.io.Reader;

Import java.io.serializable;

Import java.io.writer;

Import org.exolab.castor.xml.marshalexception;

Import org.exolab.castor.xml.marshaller;

Import org.exolab.castor.xml.unmarshaller;

Import org.exolab.castor.xml.validationException;

Import org.xml.sax.contenthandler;

/ **

* Class XmlemployEetyPE.

* @version $ revision $ $ DATE $

* /

Public class xmlemployeetype item java.io.serializable {

Private java.lang.string_empno;

Private java.lang.string _firstname; private java.lang.string _lastname

Private java.lang.string _phoneext;

Private java.lang.string _hidate;

Private java.lang.string_deptno;

Private java.lang.string_jobcode;

Private java.lang.string_jobgrade;

Private java.lang.string _jobcountry;

Private java.lang.string _salary;

Private java.lang.string _fullname;

Public XMLEMPLOYETYPE () {

Super ();

}

Public Java.lang.String getDeptno ()

{

Return THIS._DEPTNO;

}

Public java.lang.string getempno ()

{

Return THIS._EMPNO;

}

Public java.lang.string getfirstname ()

{

Return THIS._FIRSTNAME;

}

Public Java.lang.String getFullName ()

{

Return this._fullname;

}

Public Java.lang.String gethiRedate ()

{

Return this._hidate;

}

Public Java.lang.String getJobcode ()

{

Return this._jobcode;

} tjobcode ()

Public Java.lang.String getJobCountry ()

{

Return this._jobcountry;

}

Public Java.lang.String getJobgrade ()

{

Return this._jobgrade;

}

Public java.lang.string getLastname ()

{

Return THIS._LASTNAME;

}

Public Java.lang.String getphoneext ()

{

Return THIS._PHONEXT;

}

Public java.lang.string getsalary ()

{

Return this._salary;

} // - java.lang.string getsalary ()

Public boolean isvalid ()

{

Try {

Validate ();

}

Catch (org.exolab.castor.xml.validationException vex) {

Return False;

}

Return True;

}

Public void marshal (java.io.writer out)

Throws org.exolab.castor.xml.marshalexception, org.exolab.castor.xml.validationException

{

Marshaller.Marshal (this, OUT);

}

Public void marshal (org.xml.sax.contenthandler handler)

Throwsjava.io.ioException, Org.exolab.castor.xml.Marshalexception, org.exolab.castor.xml.validationException

{

Marshaller.Marshal (this, handler);

}

Public void setDeptno (java.lang.string deptno) {

THIS._DEPTNO = Deptno;

}

Public void setempno (java.lang.string empno)

{

THIS._EMPNO = Empno;

}

Public void setfirstname (java.lang.string firstname)

{

THIS._FIRSTNAME = firstName;

} // - void setFirstname (java.lang.string)

Public void setfullname (java.lang.string fullname)

{

THIS._FULLNAME = FullName;

} // - void setfullname (java.lang.string)

Public void sethiredate (java.lang.string hiredate)

{

THIS._HIREDATE = HIREDATE;

} // - void SthiRedate (java.lang.string)

Public void setjobcode (java.lang.string jobcode)

{

THIS._JOBCODE = JobCode;

} // - void setjobcode (java.lang.string)

Public void setjobcountry (java.lang.string jobcountry)

{

THIS._JOBCOUNTRY = JobCOUNTRY;

} // - void setjobcountry (java.lang.string)

Public void setjobgrade (java.lang.string jobgrade)

{

THIS._JOBGRADE = JobGrade;

} // - void setjobgrade (java.lang.string)

Public void setlastname (java.lang.string lastname)

{

THIS._LASTNAME = LastName;

} // - void setlastname (java.lang.string)

Public void setphoneext (java.lang.string phoneext)

{

THIS._PHONEXT = Phoneext;

} // - void setphoneext (java.lang.string)

Public void setsalary (java.lang.string salary)

{

THIS._SALARY = SALARY;

} // - void setsalary (java.lang.string)

Public static java.lang.Object unmarshal (java.io.reader reader)

Throws org.exolab.castor.xml.marshalexception, org.exolab.castor.xml.validationException

{

Return (com.borland.samples.xml.databaseEtype) UNMARSHALLER.Unmarshal (com.borland.samples.xml.databinding.castor.xmlemployeetype.cos, reader);

} // - java.lang.Object unmarshal (java.io.reader)

Public void validate ()

Throws org.exolab.castor.xml.validationException {

Org.exolab.castor.xml.validator validator = new org.exolab.castor.xml.validator ();

Validator.validate (this);

} // - void validate ()

}

Listing 2. Test the default data binding

/ **

* Title: XML Tutorial

* Description: XML Tutorial For JBuilder

* Company: Borland Software Coporation

* @Author

* @version 1.0

* /

Import java.io. *;

Import org.exolab.castor.xml. *;

PUBLIC CLASS DB_CASTOR {

Public db_castor () {

}

Public static void main (String [] args) {

Try {

String filename = "Employees.xml";

System.out.println ("== unmarshalling /" filename "/" == ");

// unmarshal XML file.

XMLEMPLOYEESXMLEMPLOYEES = (XMLEMPLOYEES) XMLEMPLOYEES.Unmarshal (New FileReader (FileName);

System.out.println ("TotalNumber of Xmlemployees Read =" XMLEMPLOYEES.GETXMLEMPLOYEECOUNT ());

System.out.println ("Firstxmployee's Full Name IS" XMLEMPLOYES.GETXMLEMPLOYEEEES.GETXMLEMPLOYEE (0) .getfullname ());

System.out.println ("Lastxmployee's Full Name IS" XMLEMPLOYEES.GETXMLEMPLOYEEEE (XMLEMPLOYEES.GETXMLEMPLOYEECOUNT () - 1);

// Add an Xmlemployee.

Xmlemployees.addxmployee (GetXMLEMPLOYEE ("8000", "400", "Charlie", "Castor", "

3/3/2001

"," VP "," USA "," 2 "," 1993 "," 155000.00 "));

// modify the last xmlemployee.

Xmlemployees.setXmployee (Xmlemployees.getxmlemployeeCount () - 1, getXmlemployee ("8000", "600", "Peter", "castor", "

3/3/2001

"," VP "," USA "," 3 "," 2096 "," 125000.00));

// Marshal Out the data to the Same Xml file.

XMLEMPLOYEES.Marshal (New Java.io.FileWriter (FileName);

Catch (Exception EX) {

EX.PrintStackTrace ();

}

}

private static XmlEmployee getXmlEmployee (String XmlEmployeeNumber, String departmentNumber, String firstName, String lastName, String hireDate, String jobCode, String jobCountry, String jobGrade, String phoneExt, String salary) {

XMLEMPLOYEE XMLEMPLOYEE = New XMLEMPLOYEE ();

XMLEMPLOYEE.SETEMPNO (XMLEMPLOYEENUMBER);

XMLEMPLOYE.SETDEPTNO (Departmentnumber);

XMLEMPLOYE.SETFIRSTNAME (FirstName);

XMLEMPLOYE.SETLASTNAME (Lastname);

XMLEMPLOYE.SETFULLNAME (Lastname "," firstname);

XMLEMPLOYE.SETHIREDATE (HIREDATE);

XMLEMPLOYE.SETJOBCODE (JOBCODE);

XMLEMPLOYE.SETJOBCOUNTRY (JOBCOUNTRY);

XMLEMPLOYE.SETJOBGRADE (Jobgrade);

XMLEMPLOYE.SETPHONEXT (Phoneext);

XMLEMPLOYE.SETSALY (SALARY);

Return XMLEMPLOYEE;

}

}

Finally, look at the effect of run; XML is not changed; this article only describes a Castor framework, and Jaxb (Sun's framework); and other open source frames require constant learning.

Reference: http://www-900.ibm.com/developerWorks/cn/xml/theme/x-pracdb.shtml

转载请注明原文地址:https://www.9cbs.com/read-85378.html

New Post(0)