Castor's XML data binding application

Source: Internet
Author: User
Castor's XML data binding application

Banqiao Renren http://www.jdon.com

The biggest difference between Java and. NET is that Java isSource codeBecause of the growth in the field, you don't have to wait for a company's product development progress. In the open-source field, every day, an exciting new Java technology is born. You can study it carefully, it may be a flash in the flash, but it is more likely to be the discovery of a new technology, which has forgotten your own lonely situation.

Castor is undoubtedly a new practical concept. Its XML Object operations and JDO concepts are both exciting. Here we will only talk about its XML data binding technology.

For XML operations, we already know what is the difference between sax Dom and JDOM and Castor?

Castore can map XML data to Java object data, so that you only need to operate on Java data objects, which is equivalent to operating on XML data, this concept makes it easy to use XML as a data source similar to a database.

As we know, the operating database now uses ejb jdo to perform operations on Java object data. Castor provides us with such operations on XML, isn't it more convenient? The SAS Dom JDOM is used to operate on the specific data structure in XML, without the concept of completely object-oriented like Castor.

Example:

The page. xml file is as follows:


<Homepagecollection name = "this is sample">

<Homepagecontent id = "1">

<Name> about us </Name>

<Navlink> 1.jsp</navlink>

<Icon> images/icon.gif </icon>

<Description> an in-depth look at creating applications with XML. </description>

</Homepagecontent>

<Homepagecontent id = "2">

<Name> product | service </Name>

<Navlink> 2.jsp</navlink>

<Icon> images/icon.gif </icon>

<Description> Let's Tak a look at our products. </description>

</Homepagecontent>

</Homepagecollection>

This is a typical XML file. There are multiple homepagecontent in the homepagecollection.

If you use Castor, you only need to create two objects to read and write the XML file.


Public class homepagecontent implements java. Io. serializable {

Private integer ID;

Private string name;

Private string navlink;

Private string icon;

Private string description;

Public homepagecontent (){

}


Public integer GETID () {return ID ;}

Public void setid () {This. ID = ID ;}

Public String getname () {return name ;}

Public void setname () {This. Name = Name ;}

Public String getnavlink () {return navlink ;}

Public void setlink () {This. navlink = navlink ;}

Public String getIcon () {return icon ;}

Public void seticon () {This. Icon = icon ;}

Public String getdescription () {return description ;}

Public void setdescription () {This. Description = description ;}


}

In the above JavaBean, xxxx in setxxxx and getxxxx is the corresponding name in homepagecontent in page. xml.

This is similar to the an and database ing.

Let's take a look at homepagecollection. Java


Public class homepagecollection implements java. Io. serializable {

Private string sitename;

Private list homepagecontents = new arraylist ();

Public homepagecollection (){}

// -- Manipulate the list of page objects

Public void addhomepagecontent (homepagecontent ){

Homepagecontents. Add (homepagecontent );

}

Public list gethomepagecontents (){

Return homepagecontents;

}

// -- Manipulate the name of the address book

Public String getname (){

Return sitename;

}

Public void setname (string name ){

This. sitename = Name;

}


}

Homepagecollection. Java corresponds to page. xml. Because the homepagecollection in page. xml contains multiple homepages,ProgramUsing list.

How does the data in page. XML be read into homepagecollection. Java?

Public homepagehandle (string mapfile, string xmlfile) throws exception {
This. xmlfile = xmlfile;
try {
mapping = new mapping ();
mapping. loadmapping (mapfile);

}catch (exception e) {
throw new exception (E. getmessage ();

}< P>

}

// -- page. reading data from XML into homepagecollection
Public homepagecollection read () throws exception {
homepagecollection homepages = NULL;
try {
unmarshaller UN = new unmarshaller (homepagecollection. class);
un. setmapping (Mapping);

filereader in = new filereader (xmlfile);
homepages = (homepagecollection) UN. unmarshal (in);
in. close ();

}catch (exception e) {
throw new exception (E. getmessage ();

}< br>
return homepages;

}

Note. You only need to use the unmarshaller clause to read XML data into the JavaBean homepagecollection. In this way, you only need to perform data operations on the homepagecollection. Class.

For example, you can use homepagecollection. getname () to obtain "this is sample" in page. xml ".

Is it convenient?

Note that the mapping. xml file must be configured here:



A map file for our new template System






















This file mainly allows Castor to load (unmarshaller) or unload (volume aller) data on page. xml and homepagecollection. Class homepagecontent. Class.

For further use of Castor in projects, see Chapter 4th of Java Practical System Development Guide.

 

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.