Java Reference
In-Depth Information
Unmarshalling an XML file into its Java object representation has at least two criteria:
A well-formed and valid XML file
A set of corresponding Java bindings
The Java bindings don't have to be autogenerated from the xjc command. Once
you've gained some experience with Java bindings and the annotation features, you
may prefer to create and control all aspects of Java binding by handcrafting your Java
bindings. Whatever your preference, Java's unmarshalling service utilizes the bindings
and their annotations to map XML objects to a target Java object and to map XML ele-
ments to target object fields.
Execute the example application for this recipe using this syntax, substituting pa-
tients.xml and org.java8recipes.chapter20.recipe20_6 for the re-
spective parameters:
java
org.java8recipes.chapter20.recipe20_6.UnmarshalPatients
<xmlfile><context>
20-7. Building an XML Document with
JAXB
Problem
You need to write an object's data to an XML representation.
Solution
Assuming you have created Java binding files for your XML schema as described in
Recipe 20-4, you use a JAXBContext instance to create a Marshaller object. You
then use the Marshaller object to serialize your Java object tree to an XML docu-
ment. The following code demonstrates this:
public void run(String xmlFile, String context)
throws JAXBException, FileNotFoundException {
Search WWH ::




Custom Search