Castor's XML data binding application

xiaoxiao2021-03-06  39

Castor's XML data binding application

Board bridge http://www.jdon.com 2002/07/27

Java and .NET are the biggest difference in that Java is growing in the field of open source, so you don't have to wait for a company's product development schedule, in the open source field, every day, the birth of Java exciting new Technology, you can carefully study it, it may be a new technology, it is more likely to find a new technology, this mood has made you forget your loneliness.

Castor is undoubtedly a new practical concept, and its XML objective operation and JDO concept is exciting. We only talk about its XML data binding technology here.

For XML operation, we already know what SAX DOM and JDOM, Castor have different?

CaStore maps XML data into Java object data so as long as you operate with Java data objects, it is equivalent to XML data operation, which is undoubted to make XML as a data source like a similar database.

We know, now the database is now implemented through the EJB JDO to implement data operations for Java objects. The SAS DOM JDOM is an operation of the specific data structure in XML, and there is no conceptual concept of CaStor.

for example:

There is a page.xml file as follows

About us 1.jsp images / icon.gif < Description> An in-Depth Look at CREATING Applications with XML. Product | Service 2.jsp < Icon> images / icon.gif Let's tak a look at out our products.

This is a typical XML file, there are many homepageContent in HomePageCollection.

So using CASTOR, we can read and write this XML file as long as we establish two objects.

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; navlink = navlink;} public String getIcon () {return icon;} public void setIcon () {this.icon = icon;} public String getDescription () {return description;} public void setDescription () {this.description = description; }} The above JavaBean, XXXX in SetXXXX and Getxxxx is the corresponding name in HomePageContent in page.xml.

This is very similar to the correspondence between JavaBean and the database.

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 objectspublic void addHomePageContent (Homepagecontent homepagecontent) {homepagecontents.add ( homepagecontent);} public List getHomepagecontents () {return homepagecontents;} // - manipulate the name of the address bookpublic String getName () {return SiteName;} public void setName (String name) {this.SiteName = name;}}

HomepageCollection.java is the corresponding page.xml Because the homepageCollection in page.xml contains multiple HomePage, the program is represented by list.

So how do data in page.xml read in 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 () );}} // - page.xml the data read into Homepagecollectionpublic Homepagecollection read () throws Exception {Homepagecollection homepages = null; try {Unmarshaller un = new Unmarshaller (Homepagecollection.class); un.setMapping (mapping); FileReader IN = New FileReader; HomePages = (HomepageCollection) UN.Unmarshal (in); in .close ();} catch (e.getMessage ());} return.com;} . Just use UNMARSHALLER to read XML's data into JavaBean HomePageCollection so that you can perform data operations for HomePageCollection.class.

For example, use HomePageCollection.getName () to get "this is sample" in page.xml.

Is it very convenient?

Note that you want to configure mapping.xml files:

a map file for u u t = = "homepagecontent"> < field name = "homepagecontents" type = "Homepagecontent" collection = "collection" /> this document is mainly to let Castor for page.xml and Homepagecollection.class Homepagecontent.class loading (Unmarshaller) or uninstall ( Marshaller).

About the use of Castor in the project, see "Java Practical System Development Guide" Chapter 4 Original: http://www.jdon.com/idea/castor.htm

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

New Post(0)