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.
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 (); }
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?
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