[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