近日对java对象与xml文本的相互转换有一定兴趣,于是乎在网上查看了一下相关的资料。发现了castor。 并浏览了该页面http://www.jdon.com/idea/castor.htm,但发现上面的代码有一些错漏。 自己用eclipse写了一个简单的代码如下:(主要参考了上面提到的网站的内容) 该程序是读入page.xml文件,然后转化为java对象。接着把java对象写到另外一个文件里。 **************************************************************************** 1.page.xml,要被转化为对象的页面 <?xml version="1.0" encoding="UTF-8"?> <homepagecollection name="this is sample">   <!-- 对应Homepagecollection类  -->  <homepagecontent id="1">   <!-- 对应Homepagecontent 类  -->   <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">   <!-- 对应Homepagecontent 类  -->   <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> **************************************************************************** 2.Homepagecontent.java,一个符合JavaBean规格的简单类  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) {         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;     } }
  **************************************************************************** 4.mapping.xml,映射文件,把要转化的xml文件和java类联系起来 <?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,执行转化的类  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); //读入映射文件         } catch (Exception e) {             throw new Exception(e.getMessage());         }     }     // -- page.xml中的数据读入Homepagecollection     public Homepagecollection read() throws Exception {         Homepagecollection homepages = null;         try {             Unmarshaller un = new Unmarshaller(Homepagecollection.class); // xml -> java 专用类             un.setMapping(mapping);             FileReader in = new FileReader(xmlfile);             homepages = (Homepagecollection) un.unmarshal(in); //转换             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专用类             mar.setMapping(mapping);             mar.marshal(obj);         } catch (Exception e) {             throw new Exception(e.getMessage());//转换         }         return out;     }     /**      * @param args      */     public static void main(String[] args) {         tryCastor tc = new tryCastor();         try {             //从page.xml读入数据并放到对象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("+++++++++++++++++++++++");             }                          //写到xml文本             FileWriter fw = tc.write("d.xml", hcollection);             if (null != fw) {                 fw.close();             }         } catch (Exception e) {             e.printStackTrace();         }     } }
  **************************************************************************** 小结:觉得写映射文件(mapping.xml)很麻烦,是否可以用映射来解决自动查找类字段来实现java到xml的转换?  
 
  |