|
|
Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs
Page 5 of 6
Step 4c. Validate
To marshal my Socks content tree (i.e., turn it back into XML), I must first call the validate() method on my Socks object. This validation is only necessary if I've changed the originally unmarshaled document tree. Validation ensures that
my Socks object content tree conforms to my DTD constraints.
Step 4d. Marshal
Marshaling is easy: I just create a FileOutputStream and pass it to the Socks object's marshal() method. See, I told you -- simple! I end up with an XML document on disk containing all black socks plus an extra sock. I'm
finally starting to fit in here in Europe.
Step 5. Compile and run the test program
We make sure our compiled JAXB-generated classes and the JAXB runtime jar are still in our CLASSPATH, and then compile and run. Here is the output:
Listing 7. JAXBTestSocks Output
Printing in memory socks... Sock number 1 | Name: black socks | Color: <<color value=black>> | Price: 9.99 | Smell: 7 Sock number 2 | Name: white socks | Color: <<color value=white>> | Price: 5.34 | Smell: 2 Sock number 3 | Name: old white socks | Color: <<color value=white>> | Price: 2.20 | Smell: 9 Turn all socks black for my new European look! Printing in memory socks... Sock number 1 | Name: black socks | Color: <<color value=black>> | Price: 9.99 | Smell: 7 Sock number 2 | Name: white socks | Color: <<color value=black>> | Price: 5.34 | Smell: 2 Sock number 3 | Name: old white socks | Color: <<color value=black>> | Price: 2.20 | Smell: 9 Sock number 4 | Name: new sock | Color: <<color value=black>> | Price: 5.55 | Smell: 0 Marshaling socks to file blackSocks.xml
For a full code listing and detailed instructions on running this example, download my code from Resources.
We should examine a more robust framework, since JAXB is still in such early stages. Castor, an open source offering from the Exolab Group, is based on the original JAXB specification. Castor is much more mature and is a good choice if you need XML data binding now.
Castor does everything JAXB does and more. You can use either a DTD or an XML Schema for your constraints. Furthermore, you don't need an extra binding schema document to help Castor's schema compiler. It is able to gather enough information from the XML Schema constraints alone. Sun claims that requiring an extra binding schema separates programmer info from general XML Schema info; this separation provides more flexibility. Castor allows an optional mapping file for those types of extra customizations. On top of all this, the Castor project also does object relational mapping, and Castor is working on a direct SQL-XML bridge that does not involve any Java objects.
Let's use Castor to produce a similar program that manipulates my socks. We'll follow these steps:
socks.xsd; no extra binding schema is necessary
Step 1. Start with our XML Schema, socks.xsd
We've already created our XML Schema, socks.xsd, so that's all for Step 1!