Castor learning notes (1)

xiaoxiao2021-03-06  59

1. What is CASTOR2 CASTOR is an open source software that automatically binds Java objects and XML. It can bind between Java objects, XML text, SQL data tables, and LDAP directories. 2 URL: http: // www .castor.org /

2. CaStor uses 2 Java objects refers to JavaBean with SETX and GETX methods, uses CaStor for the JavaBean specific method of use as follows: Ø

Default Usage: Default usage refers to the usage of not using the mapping format description file. *;

Import org.exolab.castor.xml. *;

Public Class Test

{

Public static void main (string [] argv) {

// build a test bean

FLightBean bean = new flightbean ();

Bean.setcarrier ("ar");

Bean.setnumber (426);

Bean.setDeparturetime ("6:

23A

");

Bean.setarrivaltime ("8:

42A

");

Try {

// Write It Out as XML

File File = New File ("Test.xml");

Writer Writer = New FileWriter (file);

Marshaller.Marshal (Bean, Writer);

// Now restore the value and list what we get

Reader Reader = New FileReader (File);

Flightbean Read = (Flightbean)

UNMARSHALLER.Unmarshal (Flightbean.class, Reader);

System.out.println ("Flight" Read.getCarrier ()

Read.getNumber () "Departing at"

Read.GetDeparturetime ()

"and arriving at" read.getarrivaltime ());

} catch (ioException ex) {

EX.PrintStackTrace (System.err);

} catch (MARSHALEXCEPTION EX) {

EX.PrintStackTrace (System.err);

} catch (validationException ex) {

EX.PrintStackTrace (System.err);

}

}

} Ø Standard usage: import java.io. *;

Import org.exolab.castor.xml. *;

Import org.exolab.castor.mapping. *;

Public Class Test

{

Public static void main (string [] argv) {

// build a test bean

FLightBean bean = new flightbean ();

Bean.setcarrier ("ar");

Bean.setnumber (426);

Bean.setDeparturetime ("6:

23A

");

Bean.setarrivaltime ("8:

42A

");

Try {

// Write it out as xmlmapping map = new mapping ();

Map.LoadMapping ("mapping.xml");

File File = New File ("Test.xml");

Writer Writer = New FileWriter (file);

Marshaller Marshaller = New Marshaller (Writer);

Marshaller.Setmapping (MAP);

Marshaller.Marshal (bean);

// Now restore the value and list what we get

Reader Reader = New FileReader (File);

UNMARSHALLER UNMARSHALLER = New Unmarshaller (MAP);

Flightbean Read = (Flightbean) UNMARSHALLER.Unmarshal (Reader);

System.out.println ("Flight" Read.getCarrier ()

Read.getNumber () "Departing at"

Read.getDeparturetime () "And Arriving At" Read.GetarrivalTime ());

} catch (ioException ex) {

EX.PrintStackTrace (System.err);

} catch (MARSHALEXCEPTION EX) {

EX.PrintStackTrace (System.err);

} catch (validationException ex) {

EX.PrintStackTrace (System.err);

}

Catch (mappingexception ex) {

EX.PrintStackTrace (System.err);

}

}

}

Ø: The XML file generated by the default usage is as follows:

8:

42A

6:

23A

ar

Means of

• An attribute for creating an element for an attribute having a basic type value (this example is disclosed in the int value) by the getNumber () method).

• A child element of the root element (all other properties in this example because they are strings).

Ø The XML file generated by the standard method is as follows:

This is usually our expectation, so that it is only to generate the format we expect because we use the map format description file. It is also an XML file:

"- // EXOLAB / CASTOR MAPPING DTD VERSION 1.0 // en"

"http://castor.exolab.org/mapping.dtd">

Basic Mapping Example

· Note Class Class

Name in the label should be a full name with the packaged JavaBean class

· Map-to XML = "flight"

Refers to the root node of the XML file, case sensitive

· AUTO-Complete = "false" or Number attribute when there is no auto-complete property

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

New Post(0)