Jaxb2.0 implements Java Object conversion Xml Conversion Java Object.

Source: Internet
Author: User

[Java]
// Test class
[Java]
<Pre class = "java" name = "code"> import com. toft. webservice. client. domino. DominoObject;
Import com. toft. webservice. core. JaxbBinder;
 
 
Public class JaxbTest {

Private static final String DECLARATION = "<? Xml version = '1. 0' encoding = 'utf-8'?> <! DOCTYPE document SYSTEM 'xmlschemas/domino_7_0_1.dtd '> ";
Private static final String ENCODING = "UTF-8 ";
Private DominoObject dominoObject = new DominoObject ();
Public static void main (String [] args ){

}

/**
* Use JAXB to generate XML that complies with the interface.
* @ Return String
* @ Throws Exception
*/
Private String getDominoXml () throws Exception {
Try {
JaxbBinder binder = new JaxbBinder (DominoObject. class );
Return binder. toXml (dominoObject, ENCODING, DECLARATION );
} Catch (Exception e ){
Throw e;
}
}
}
</Pre> <br>
<Pre> </pre>
<Pre class = "java" name = "code"> </pre> <pre class = "java" name = "code"> // tool class </pre> <pre class = "java" name = "code"> import java. io. stringReader;
Import java. io. StringWriter;
 
Import javax. xml. bind. JAXBContext;
Import javax. xml. bind. JAXBException;
Import javax. xml. bind. Marshaller;
Import javax. xml. bind. Unmarshaller;
 
/**
* Use Jaxb2.0 to implement the XML <-> Java Object Binder.
*
* @ Author Sun zuqiang
*/
Public class JaxbBinder {
// Multi-threaded secure Context.
Private JAXBContext jaxbContext;
 
/**
* @ Param types all types of Root objects to be serialized.
*/
Public JaxbBinder (Class <?>... Types ){
Try {
JaxbContext = JAXBContext. newInstance (types );
} Catch (JAXBException e ){
Throw new RuntimeException (e );
}
}
 
/**
* Java Object-> Xml.
*/
Public String toXml (Object root ){
Try {
StringWriter writer = new StringWriter ();
CreateMarshaller ("UTF-8", null, true). marshal (root, writer );
Return writer. toString ();
} Catch (JAXBException e ){
Throw new RuntimeException (e );
}
}
 
/**
* Java Object-> Xml.
*/
Public String toXml (Object root, String encoding ){
Try {
StringWriter writer = new StringWriter ();
CreateMarshaller (encoding, null, false). marshal (root, writer );
Return writer. toString ();
} Catch (JAXBException e ){
Throw new RuntimeException (e );
}
}
 
/**
* Java Object-> Xml.
*/
Public String toXml (Object root, String encoding, String declaration ){
Try {
StringWriter writer = new StringWriter ();
Writer. append (declaration );
CreateMarshaller (encoding, declaration, false). marshal (root, writer );
Return writer. toString ();
} Catch (JAXBException e ){
Throw new RuntimeException (e );
}
}
 
/**
* Xml-> Java Object.
*/
@ SuppressWarnings ("unchecked ")
Public <T> T fromXml (String xml ){
Try {
StringReader reader = new StringReader (xml );
Return (T) createUnmarshaller (). unmarshal (reader );
} Catch (JAXBException e ){
Throw new RuntimeException (e );
}
}
 
/**
* Create a exploraller, encoding (which can be Null), and declaration (which can be Null ).
*/
Public extends aller createMarshaller (String encoding, String declaration, Boolean formatted ){
Try {
Extends aller extends aller = jaxbContext. createMarshaller ();
 
// Whether to format XML
If (formatted ){
Marshaller. setProperty (Marshaller. JAXB_FORMATTED_OUTPUT, Boolean. TRUE );
}
 
// Set the encoding method
If (null! = Encoding &&! "". Equals (encoding )){
Extends aller. setProperty (extends aller. JAXB_ENCODING, encoding );
}
 
// Set the XML Declaration
If (null! = Declaration &&! "". Equals (declaration )){
Extends aller. setProperty ("com. sun. xml. bind. xmlDeclaration", Boolean. FALSE );
}
Return marshaller;
} Catch (JAXBException e ){
Throw new RuntimeException (e );
}
}
 
/**
* Create UnMarshaller.
*/Www.2cto.com
Public Unmarshaller createUnmarshaller (){
Try {
Return jaxbContext. createUnmarshaller ();
} Catch (JAXBException e ){
Throw new RuntimeException (e );
}
}
 
}
</Pre> <br>
Author: sunzuqiang

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.