Create and parse XML file Java instance

Source: Internet
Author: User

/* XMLTest. java
* Created on 2004-11-16
*/
Package test;

Import java. beans. XMLEncoder;
Import java. io. BufferedInputStream;
Import java. io. BufferedOutputStream;
Import java. io. FileInputStream;
Import java. io. FileOutputStream;
 
Public class XMLTest {
Public void xmlEncode () throws Exception
{
MyInfo my = new MyInfo ();
My. setMyAge (25 );
My. setMyName ("google ");
My. setMyAddress ("china ");
My. setMyEducation ("master in science ");
 
XMLEncoder encoder = new XMLEncoder (
New BufferedOutputStream (
New FileOutputStream ("myinfo. xml ")));
Encoder. writeObject (my );
Encoder. close ();
System. out. println (my );
}
 
Public void xmlDecode () throws Exception
{
Java. beans. XMLDecoder decoder = new java. beans. XMLDecoder (
New BufferedInputStream (new FileInputStream ("myinfo. xml ")));
MyInfo my = (MyInfo) decoder. readObject ();
Decoder. close ();
System. out. println (my );
System. out. println ("Your age:" + my. getMyAge ());
System. out. println ("Your name:" + my. getMyName ());
System. out. println ("Your Address:" + my. getMyAddress ());
System. out. println ("Your Education:" + my. getMyEducation ());
}
Public static void main (String args []) throws Exception {
XMLTest st = new XMLTest ();
St. xmlEncode ();
St. xmlDecode ();
}
}

The corresponding helper class MyInfo code is as follows:

Package test;

/**
* Add one sentence class summary here.
* Add class description here.
*
* @ Author lxx
* @ Version 1.0
*/

Public class MyInfo {
Private int myAge;
Private String myName;
Private String myAddress;
Private String myEducation;

Public MyInfo (){}

Public int getMyAge (){
Return myAge;
}
Public void setMyAge (int age ){
This. myAge = age;
}


Public String getMyName (){
Return myName;
}
Public void setMyName (String name ){
This. myName = name;
}


Public String getMyAddress (){
Return myaddress;
}
Public void setmyaddress (string address ){
This. myaddress = address;
}



Public String getmyeducation (){
Return myeducation;
}
Public void setmyeducation (string Education ){
This. myeducation = education;
}

 

}

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.