Mapping between XML in Java and entity classes

Source: Internet
Author: User

Entity class:

1  PackageXML;2 3 Importjavax.xml.bind.annotation.XmlRootElement;4 5 @XmlRootElement6  Public classUser {7 String name;8 String English_name;9 String age;Ten String sex; One String address; A String description; -      PublicUser () {} -      the      PublicUser (string name, String english_name, String age, String sex , - string address, string description) { -          This. name=name; -          This. english_name=English_name; +          This. age=Age ; -          This. sex=sex; +          This. address=address; A          This. description=description; at     } -      PublicString GetName () { -         returnname; -     } -      Public voidsetName (String name) { -          This. Name =name; in     } -      PublicString Getenglish_name () { to         returnEnglish_name; +     } -      Public voidsetenglish_name (String english_name) { the          This. English_name =English_name; *     } $      PublicString getage () {Panax Notoginseng         returnAge ; -     } the      Public voidsetage (String age) { +          This. Age =Age ; A     } the      PublicString Getsex () { +         returnsex; -     } $      Public voidsetsex (String sex) { $          This. Sex =sex; -     } -      PublicString getaddress () { the         returnaddress; -     }Wuyi      Public voidsetaddress (String address) { the          This. Address =address; -     } Wu      PublicString getdescription () { -         returndescription; About     } $      Public voidsetdescription (String description) { -          This. Description =description; -     } -      A      +      the}

I. Translating Java objects into XML files

1  PackageXML;2 3 ImportJava.io.File;4 5 ImportJavax.xml.bind.JAXBContext;6 Importjavax.xml.bind.JAXBException;7 ImportJavax.xml.bind.Marshaller;8 9 /**Ten * Mapping classes to XML One  * @authorAdministrator A  * -  */ -  Public classDemo1 { the      Public Static voidMain (string[] args) { -         //create an XML object and save it in the specified location -File File =NewFile ("D:\\javaitem\\xml\\src\\xml\\1.xml"); -         //declare a Jaxbcontext object + Jaxbcontext Jaxbcontext; -         Try { +             //Specifies the mapped class, creating the context of the Jaxbcontext object AJaxbcontext = Jaxbcontext.newinstance (User.class); at             //Create a Conversion object Marshaller -Marshaller m =Jaxbcontext.createmarshaller (); -             //to create data in an XML file -User User =NewUser ("Zhang San", "Zhangsnan", "30", "Male", "Hefei, Anhui", "software Engineer")); -             //to convert a user object in a Java class to XML - m.marshal (user, file); inSYSTEM.OUT.PRINTLN ("Conversion complete"); -}Catch(jaxbexception e) { to e.printstacktrace (); +         } -     } the}

Two. converting XML files to Java objects

1  PackageXML;2 3 ImportJava.io.File;4 5 ImportJavax.xml.bind.JAXBContext;6 Importjavax.xml.bind.JAXBException;7 ImportJavax.xml.bind.Unmarshaller;8 9 Ten  One  Public classDemo2 { A      Public Static voidMain (string[] args) { -File file=NewFile ("D:\\javaitem\\xml\\src\\xml\\1.xml"); - Jaxbcontext Jaxbcontext; the         Try { -Jaxbcontext=jaxbcontext.newinstance (User.class); -Unmarshaller u=Jaxbcontext.createunmarshaller (); -User user=(User) u.unmarshal (file); +System.out.println ("Name:" +user.getname ()); -System.out.println ("English name:" +user.getenglish_name ()); +System.out.println ("Age:" +user.getage ()); ASystem.out.println ("Gender:" +user.getsex ()); atSYSTEM.OUT.PRINTLN ("Address:" +user.getaddress ()); -System.out.println ("Description:" +user.getdescription ()); -}Catch(jaxbexception e) { - e.printstacktrace (); -         } -     } in}

Mapping between XML in Java and entity classes

Related Article

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.