Convert xml to javaBean to xml

Source: Internet
Author: User

1. first define javaBeanpackage com. wutka. jox. test; import com. wutka. jox. *; import java. util. *; public class TestBean implements java. io. serializable {protected int foo; protected String bar; protected java. util. date baz; protected Vector thingies; protected TestSubbean subbean; public TestBean () {bar = ""; baz = new Date (); thingies = new Vector ();} public int getFoo () {return foo;} public void setFo O (int aFoo) {foo = aFoo;} public String getBar () {return bar;} public void setBar (String aBar) {bar = aBar;} public java. util. date getBaz () {return baz;} public void setBaz (java. util. date aBaz) {baz = aBaz;} public TestSubbean getSub () {return subbean;} public void setSub (TestSubbean aSub) {subbean = aSub;} public String [] getThingies () {String [] retThingies = new String [thingies. s Ize ()]; if (thingies. size ()> 0) thingies. copyInto (retThingies); return retThingies;} public void setThingies (String [] newThingies) {thingies = new Vector (newThingies. length); for (int I = 0; I <newThingies. length; I ++) {thingies. addElement (newThingies [I]);} public String getThingies (int I) {return (String) thingies. elementAt (I);} public void setThingies (int I, String thingy) {thi Ngies. setElementAt (thingy, I);} public String toString () {StringBuffer ret = new StringBuffer ("foo =" + foo + "; bar =" + bar + "; baz = "+ baz. toString () + "; thingies ="); for (int I = 0; I <thingies. size (); I ++) {if (I> 0) ret. append (","); ret. append (String) thingies. elementAt (I);} ret. append ("; sub ="); ret. append (subbean. toString (); return ret. toString () ;}} 2. xml file <? Xml version = "1.0"?> <MarkTest> <thingies> Network </thingies> <thingies>: </thingies> <thingies> /thingies> <foo> 5 </foo> <baz> 6/25/00 AM </baz> <bar> This is the website value </bar> <sub> <age> 1 </age> <name> wangdei </name> </sub> </MarkTest> The following program converts xml into javanpackage com. wutka. jox. test; import com. wutka. jox. *; import java. io. *; public class TestDeser {public static void main (String [] args) {try {FileInputStream in = new FileInputStream ("bean. xml "); JOXBeanInputStream joxIn = new JOXBeanInputStream (in); TestBean testBean = (TestBean) joxIn. readObject (TestBean. class); System. out. println (testBean);} catch (Exception exc) {exc. the program in printStackTrace () ;}} is to convert javaBean into xmlpackage com. wutka. jox. test; import com. wutka. jox. *; import java. io. *; public class TestSer {public static void main (String [] args) {try {TestBean B = new TestBean (); B. setFoo (5); B. setBar ("This is the bar value"); B. setThingies (new String [] {"", ""}); TestSubbean sub = new TestSubbean (); sub. setName ("Mark"); sub. setAge (35); B. setSub (sub); FileOutputStream fileOut = new FileOutputStream ("bean. xml "); JOXBeanOutputStream joxOut = new JOXBeanOutputStream (fileOut); joxOut. writeObject ("MarkTest", B); joxOut. close ();}}}

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.