近日对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>lets 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的转换?
