Convert a class to XML using the JDK's own tools

Source: Internet
Author: User
first, the use of scenarios

At some point, we need to convert the class to an XML string or file to send or manipulate, and the JDK itself provides such a tool. Jaxbcontext. second, the use

The comments in the code are clear, so you don't have to explain. There are two main classes: Personbo is the carrier class to be converted. Main is the test class.

Personbo

Package com.kingboy.xml;
Import javax.xml.bind.annotation.*;

Import java.util.List;
 /** * @author kingboy--KingBoyWorld@163.com * @date 2017/11/12 PM 2:18 * @desc XML conversion data carrier. *//defines whether the attribute or Get/set is mapped to an XML file.
If there is no word will be the error, because the default will both processing properties and processing Get/set, the error prompts appear two times. The name of the root node of the @XmlAccessorType (value = Xmlaccesstype.field)//xml @XmlRootElement (name = "Person")//Specify the order of properties in the XML file @
        XmlType (Proporder = {"id", "realname", "nickname", "Address", "Phonenum", ' Families '}) public class Personbo {public Personbo () {} public Personbo (String desc, Integer ID, string real Name, string nickname, String address, string phonenum, list<string> families) {this.de
        sc = desc;
        This.id = ID;
        This.realname = Realname;
        This.nickname = nickname;
        this.address = address;
        This.phonenum = Phonenum;
    This.families = families; }//Root node properties, for example: <person desc= "Properties" ></person> @XmLattribute (name = "desc") Private String desc;

    The child nodes in the XML @XmlElement (name = "Real_name", required = true, DefaultValue = "No_name") private String realname;

    @XmlElement (name = "id", required = true, DefaultValue = "no_id") private Integer ID;

    @XmlElement (name = "Nick_name") private String nickname;

    @XmlElement (name = ' address ') private String address;

    @XmlElement (name = "Phone_num", required = true) private String phonenum;

    @XmlList @XmlElement (name = "Family_num") private list<string> families;
    Public String GetDesc () {return desc;
        Public Personbo Setdesc (String desc) {THIS.DESC = desc;
    return this;
    Public String Getrealname () {return realname;
        Public Personbo setrealname (String realname) {this.realname = Realname;
    return this;
    Public Integer GetId () {return id; Public Personbo setId (Integer ID) {this.id = ID;
    return this;
    Public String Getnickname () {return nickname;
        Public Personbo Setnickname (String nickname) {this.nickname = nickname;
    return this;
    Public String getaddress () {return address;
        Personbo setaddress (String address) {this.address = address;
    return this;
    Public String Getphonenum () {return phonenum;
        Public Personbo Setphonenum (String phonenum) {this.phonenum = Phonenum;
    return this;
    Public list<string> getfamilies () {return families;
        Public Personbo setfamilies (list<string> families) {this.families = families;
    return this;
                @Override public String toString () {return "personbo{" + "id=" + ID +
       ", realname= '" + realname + ' + ", nickname= '" + nickname + ' \ "+         ", address= '" + address + ' \ ' + ", phonenum= '" + phonenum + ' \ ' + ", families="
    + Families + '} ';
 }
}

Main

Package com.kingboy.xml;
Import Javax.xml.bind.JAXBContext;
Import javax.xml.bind.JAXBException;
Import Javax.xml.bind.Marshaller;
Import java.io.*;
Import java.util.List;
Import java.util.stream.Collectors;

Import Java.util.stream.Stream;
 /** * @author kingboy--KingBoyWorld@163.com * @date 2017/11/12 pm 2:42 * @desc Conversion class. */public class Main {public static void main (string[] args) {//Prepare an Entity Data list<string> Familie
        s = Stream.of ("Father", "Mother", "sister"). Collect (Collectors.tolist ());
        Personbo Personbo = new Personbo ("Man", 1, "Golden", "Kingboy", "Beijing", "13288888888", families);
        Convert String XML = Beantoxml (Personbo);
    SYSTEM.OUT.PRINTLN (XML); The/** * Bean is converted to XML and, if the conversion fails, returns an empty string * @param object * @return/public static string Beantoxml ( Object) {try {//Create action class Jaxbcontext Jax = Jaxbcontext.newinstance (Object.getclass ()
            ); Marshaller Marshaller = Jax.createmarshaller ();
            Parse StringWriter SW = new StringWriter ();
            Format Marshaller.setproperty (Marshaller.jaxb_formatted_output, true);
            Marshaller.marshal (object, SW);
        return sw.tostring ();
        catch (Jaxbexception e) {e.printstacktrace ();
    Return "";
 }

}

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.