Today's note series CASTOR

xiaoxiao2021-03-06  20

Recently, there is a certain interest in the mutual conversion of Java objects and XML text, so I look at the relevant information online. Discovered Castor.

And browsed this page http://www.jdon.com/idea/castor.htm, but found that the above code has some wrong leaks.

I wrote a simple code with Eclipse as follows: (Mainly referring to the contents of the website mentioned above)

The program is read into the Page.xml file and then converts to a Java object. Then write the Java object to another file.

*********************************************************** **********************************

1.Page.xml, page to be converted to object

About US 1.jsp images / icon.gif an in-depth look at create application with xml. Product | Service 2.jsp Images /icon.gif Let's tak a look at out our product>

*********************************************************** **********************************

2.HomePageContent.java, a simple class that conforms to the JavaBean specification

Package tryforcastor;

Public class homepagecontent implements java.io.serializable {

PRIVATE STATIC FINAL Long SerialVersionuid = 3689909565688657717L;

Private integer ID;

PRIVATE STRING NAME;

Private string navlink; private string icon;

Private string description;

Public HomePageContent () {}

Public integer getId () {returnid;}

Public void setid (integer ID) {this.id = ID;}

Public string getname () {return name;} public void setname (String name) {this.name = name;} public string getnavlink () {Return Navlink;

Public void setnavlink (string navlink) {this.navlink = navlink;}

Public string geticon () {return}

Public void seticon (string icon) {this.icon = icon;

Public string getDescription;} {Return Description;

Public void setdescription (string description) {this.description = description;

}

*********************************************************** **********************************

3.HomePageCollection.java,

Package tryforcastor;

Import java.util. *;

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); HomePageContent;

Public List GethomePageContents () {Return HomePageContent

// - manipulate the name of the address book public string getName () {Return SiteName;}

Public void setname (string name) {this.sitename = name;}

}

*********************************************************** **********************************

4. mapping.xml, mapping file, link the XML file you want to transform and Java class

a map file for u u t = = "homepagecontent"> < Field name = "HomePageContent" type = "homepagecontent" collection = "collect" /> *************************** *********************************************************** **

5.Trycastor.java, execute the transformation class

Package tryforcastor;

Import java.io.fileReader; import java.io.filewriter; import java.util. *;

Import org.exolab.castor.mapping.mapping; import org.exolab.castor.xml.Marshaller; import org.exolab.castor.xml.unmarshaler;

/ ** * @Author hbm * * / public class trycastor {public mapping mapping;

Public string xmlfile;

public void HomePageHandle (String mapfile, String xmlfile) throws Exception {this.xmlfile = xmlfile; try {mapping = new Mapping (); mapping.loadMapping (mapfile); // read mapping file} catch (Exception e) {throw new EXCEPTION (E.GetMessage ());}}

// - page.xml the data read into Homepagecollection public Homepagecollection read () throws Exception {Homepagecollection homepages = null; try {Unmarshaller un = new Unmarshaller (Homepagecollection.class); // xml -> java special class un.setMapping (mapping);

FileReader IN = New FileReader; HomePages = (HomePageCollection) un.unmarshal (in); // Convert in.close ();} catch (exception e) {throw new exception (E.GetMessage ());} return Homepages;

// hbm add public FileWriter write (String outFile, Object obj) throws Exception {FileWriter out = new FileWriter (outFile); try {Marshaller mar = new Marshaller (out); // java-> xml special class mar.setMapping (mapping ); Mar.Marshal (OBJ); Catch (Exception E) {throw new exception (E.getMessage ()); // conversion} return out;}

/ ** * @Param args * / public static void main (String [] args) {Trycastor TC = New Trycastor (); try {// read data from page.xml and put it in the object hcollection tc.homepagehandle (" Mapping.xml, "page.xml"); HomePageCollection hcollection = tc.read (); list list = hcollection.getHomePageContents (); for (item.hasnext (); itute .hasnext ();) {HomePageContent H = (HOMEPAGECONTENT) ITER.NEXT (); system.out.println (h.getdescription ()); system.out.println (h.geticon ()); system.out.println (h.getname ()); System.out.println (h.getnavlink ()); system.out.println (h.getname2 ()); system.out.println (h.getid ()); system.out.println (h.getclass () ); System.out.println ( ");} // Write to XML Text FileWriter FW = Tc.write (" D.xml ", hcollection; if (null! = fw) {fw.close ();}} catch (exception e) {E.PrintStackTrace ();}

}

}

*********************************************************** **********************************

Summary: I feel that the write file (mapping.xml) is cumbersome, can you use a mapping to solve the automatic lookup class field to implement the conversion of Java to XML?

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

New Post(0)