近日對Java對象與XML文本的相互轉換有一定興趣,於是乎在網上查看了一下相關的資料。發現了castor。
並浏覽了該頁面http://www.jdon.com/idea/castor.htm,但發現上面的代碼有一些錯漏。
自己用eclipse寫了一個簡單的代碼如下:(主要參考了上面提到的網站的內容)
該程序是讀入page.XML文件,然後轉化為java對象。接著把Java對象寫到另外一個文件裡。
****************************************************************************
1.page.XML,要被轉化為對象的頁面
****************************************************************************
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類聯系起來
****************************************************************************
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的轉換?