O'Reilly logo

Java & XML Data Binding by Brett McLaughlin

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required