The Castor of today's Notes series

Source: Internet
Author: User
Notes
Recently, there is a certain interest in the conversion of Java objects and XML text, so we have a look at the relevant information online. Found the Castor.

and browsed the page http://www.jdon.com/idea/castor.htm, but found that the above code has some errors and omissions.

I wrote a simple code with eclipse as follows: (mainly refer to the content of the website mentioned above)

The program is read into the Page.xml file and then converted to a Java object. Then write the Java object in a different file.

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

1.page.xml, the page to be converted into an object

<?xml version= "1.0" encoding= "UTF-8" >
****************************************************************************

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 () {return id; }

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 icon; }

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 static final long serialversionuid = 3545239128603309878L;

Private String SiteName;

Private List homepagecontents = new ArrayList ();

Public homepagecollection () {}

--Manipulate the List of Page objects public void addhomepagecontent (Homepagecontent homepagecontent) {Home    Pagecontents.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; }

}

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

4.mapping.xml, mapping files, linking XML files to transformations and Java classes

<?xml version= "1.0" encoding= "UTF-8"? ><mapping> <description>a Map file for our new template System</description> <class name= "homepagecontent" >  <map-to xml= "Homepagecontent" />  <field name= "id" type= "integer" >   <bind-xml name= "id" node= "attribute"/ >  </field>  <field name= "Name" type= "string"/>  <field name= " NavLink "type=" string "/>  <field name=" icon "type=" string "/>  <field name=" Description "type=" string "/> </class> <class name=" Homepagecollection ">  < Map-to xml= "homepagecollection"/>  <field name= "name" type= "string" >   < Bind-xml name= "name" node= "attribute"/>  </field>  <field name= "Homepagecontents" Type= "Homepagecontent"    collection= "collection"/> </class></mappinG>

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

5.trycastor.java, performing the transformation of the 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.Unmarshaller;

/** * @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 into the mapping file} catch (Exception e) {throw new Exception (E.getmessage ()); }

}

--The data in Page.xml is read into homepagecollection public homepagecollection read () throws Exception {homepagecollection ho        Mepages = null; try {unmarshaller un = new Unmarshaller (homepagecollection.class);//XML-> Java private class Un.setmapp ing (mapping);

FileReader in = new FileReader (xmlfile); 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 FileWrite        R (outfile);            try {Marshaller mar = new Marshaller (out);//java-> XML Private Class mar.setmapping (mapping);        Mar.marshal (obj);    catch (Exception e) {throw new Exception (E.getmessage ());//convert} return out; }

   /**     * @param args     */    Public static void Main (string[] args) {        trycastor tc = new Trycastor ();         try {            // Read the data from the Page.xml and put it in the object hcollection              TC. Homepagehandle ("Mapping.xml", "Page.xml");            Homepagecollection hcollection = Tc.read ();            List List = Hcollection.gethomepagecontents ();            for ( Iterator iter = List.iterator (); Iter.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: Think write mapping file (mapping.xml) is very troublesome, can you use mapping to solve the automatic lookup class fields to implement Java to XML conversion?




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.