Converting to XML
Once you’re ready to actually perform the conversion
to XML, invoking a marshal()
method is about as
simple as it gets. In this section, I’ll continue to
use the MoviesServlet
introduced in Chapter 4 and demonstrate how changes can be made and
marshalled back out to XML. This will give you a clear idea of how
marshalling works in a realistic way.
Java Input
All that you need for Java input is a set of object instances from JAXB-generated classes. The movie database classes fit the bill, and the instances unmarshalled from the last chapter are perfect candidates. Before bothering to convert these back to XML, though, it makes sense to allow the user to change the values (otherwise, what is the point of marshalling?).
The server
To accommodate modification of the movie database, it is possible to add some new actions to the servlet to complement the “list” action already handled. First, add a few import statements to the class:
import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; import java.io.PrintWriter; import java.util.Iterator; import java.util.List; // Servlet imports import javax.servlet.ServletConfig; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; // JAXB imports import javax.xml.bind.StructureValidationException; // Movie database generated classes ...
Get Java & XML Data Binding now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.