XML strings and Java entity classes convert Jaxbxmlutil Tool class with Java entity class generate SOAP Interface message case

Source: Internet
Author: User

Jaxbxmlutil Tool Class

 PackageCom.aiait.ivs.util;ImportJava.io.StringReader;ImportJava.io.StringWriter;ImportJavax.xml.bind.JAXBContext;ImportJavax.xml.bind.Marshaller;ImportJavax.xml.bind.Unmarshaller;/*** JAXB Tool class XML and Java classes convert each other * *@authorSunjianbo * @date April 17, 2017*/ Public classJaxbxmlutil { Public Static FinalString default_encoding = "UTF-8"; /*** Pojo converted to XML default encoding UTF-8 * *@paramobj object to be converted *@returnXML format string *@throwsException jaxbexception*/     Public StaticString converttoxml (Object obj)throwsException {returnconverttoxml (obj, default_encoding); }    /*** Pojo converted to XML * *@paramobj object to be converted *@paramEncoding Code *@returnXML format string *@throwsException jaxbexception*/     Public StaticString Converttoxml (Object obj, string encoding)throwsException {String result=NULL; Jaxbcontext Context=jaxbcontext.newinstance (Obj.getclass ()); Marshaller Marshaller=Context.createmarshaller (); //Specifies whether to use wrap and indent property names that are formatted for the grouped XML data. Marshaller.setproperty (Marshaller.jaxb_formatted_output,true);        Marshaller.setproperty (marshaller.jaxb_encoding, ENCODING); StringWriter writer=NewStringWriter ();        Marshaller.marshal (obj, writer); Result=writer.tostring (); returnresult; }    /*** XML converted to JavaBean * *@paramXML XML format string *@paramT objects to be converted *@returnConverted Object *@throwsException jaxbexception*/@SuppressWarnings ("Unchecked")     Public Static<T> T Converttojavabean (String XML, class<t> t)throwsException {T obj=NULL; Jaxbcontext Context=jaxbcontext.newinstance (t); Unmarshaller Unmarshaller=Context.createunmarshaller (); Obj= (T) Unmarshaller.unmarshal (NewStringReader (XML)); returnobj; }}

Entity classes that generate XML

ImportJavax.xml.bind.annotation.XmlAccessType;ImportJavax.xml.bind.annotation.XmlAccessorType;ImportJavax.xml.bind.annotation.XmlAttribute;Importjavax.xml.bind.annotation.XmlElement;Importjavax.xml.bind.annotation.XmlRootElement;ImportJavax.xml.bind.annotation.XmlType, @XmlAccessorType (Xmlaccesstype.field) @XmlType (name= "", Proporder = {        "Header",        "Body"}) @XmlRootElement (name= "Soapenv:envelope") Public classrequestentity {@XmlAttribute (name= "Xmlns:soapenv")    protectedString soapenv= "http://schemas.xmlsoap.org/soap/envelope/"; @XmlAttribute (Name= "Xmlns:eapp")    protectedString eapp= "Http://wx.gmw9.com"; @XmlAttribute (Name= "Xmlns:bus")    protectedString bus=NULL; @XmlElement (Required=true, name= "Soapenv:header")    protectedRequestheader Header; @XmlElement (Required=true, name= "Soapenv:body")    protectedrequestbody body; //Get Set method omitted    }ImportJavax.xml.bind.annotation.XmlAccessType;ImportJavax.xml.bind.annotation.XmlAccessorType;Importjavax.xml.bind.annotation.XmlElement;Importjavax.xml.bind.annotation.XmlRootElement, @XmlAccessorType (Xmlaccesstype.field) @XmlRootElement (name= "Soapenv:body") Public classRequestbody {@XmlElement (required=true, name= "EAPP:AAAAAA")     Publicbodycontent aaaaaaa//Get Set method omitted    }ImportJavax.xml.bind.annotation.XmlAccessType;ImportJavax.xml.bind.annotation.XmlAccessorType;Importjavax.xml.bind.annotation.XmlElement;Importjavax.xml.bind.annotation.XmlRootElement;ImportJavax.xml.bind.annotation.XmlType, @XmlAccessorType (Xmlaccesstype.field) @XmlType (name= "", Proporder = {    "Aa1",    "Aa2",    "Aa3"}) @XmlRootElement (name= "EAPP:AAAAAA") Public classbodycontent {@XmlElement (required=true, name= "Eapp:compno")        protectedString Aa1; @XmlElement (Required=true, name= "Eapp:agentcode")        protectedString Aa2; @XmlElement (Required=true, name= "Eapp:eappid")        protectedString Aa3; //Get Set method omitted    }

The resulting XML is as follows

<Soapenv:envelopeXmlns:soapenv= "http://schemas.xmlsoap.org/soap/envelope/"Xmlns:eapp= "Http://wx.gmw9.com"><Soapenv:header></Soapenv:header><Soapenv:body><Eapp:aaaaaa><Eapp:aa1>041</eapp:aa1> <eapp:aa2>5000</eapp:aa2> <eapp:aa3>? </eapp:aa3> </< Span style= "COLOR: #800000" >eapp:aaaaaa> </soapenv:body></soapenv:envelope>       

XML strings and Java entity classes convert Jaxbxmlutil Tool class with Java entity class generate SOAP Interface message case

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.