The reading that I've done thus far on JAXB suggests that its usage is limited to serializing classes that one can annotate properly (i.e. one has the class' source).
Is there a way to use JAXB to ser开发者_如何学JAVAialize 3rd-party class (i.e. one does not have the source) using reflection?
At this point, I'm doing this manually.
Note: I'm the EclipseLink JAXB (MOXy) lead, and a member of the JAXB 2 (JSR-222) expert group.
EclipseLink JAXB (MOXy) offers an extension that enables you to represent your metadata as XML allow which is necessary when mapping third party classes:
Sample
<?xml version="1.0"?>
<xml-bindings
xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm"
package-name="blog.bindingfile">
<xml-schema
namespace="http://www.example.com/customer"
element-form-default="QUALIFIED"/>
<java-types>
<java-type name="Customer">
<xml-root-element/>
<xml-type prop-order="firstName lastName address phoneNumbers"/>
<java-attributes>
<xml-element java-attribute="firstName" name="first-name"/>
<xml-element java-attribute="lastName" name="last-name"/>
<xml-element java-attribute="phoneNumbers" name="phone-number"/>
</java-attributes>
</java-type>
<java-type name="PhoneNumber">
<java-attributes>
<xml-attribute java-attribute="type"/>
<xml-value java-attribute="number"/>
</java-attributes>
</java-type>
</java-types>
</xml-bindings>
For More Information
- http://bdoughan.blogspot.com/2010/12/extending-jaxb-representing-annotations.html
- http://bdoughan.blogspot.com/2011/06/moxy-extensible-models-multiple.html
- http://bdoughan.blogspot.com/2011/06/using-eclipselink-moxy-with-metadata.html
I'm not sure, but you might wanna look at xstream (http://x-stream.github.io/) if you just try to serialize and deserialize objects to XML and back.
Greetings -Sascha-
精彩评论