Chapter 10. Development of XML-bas ed Enterprise applications 229
DocumentBuilder to use for the document generation. It creates a
customer element representing the document root. It adds that element to
the created document, and starts creating elements for the customer's
attributes, adding them to the document. It retrieves that values for these
attributes from the customer JavaBean, using the bean's Get methods.
After the document construction with the data, the method returns the
document.
Figure 10-13 Produce DOM Document from JavaBean
Example 10-1 Source code for CustomerXML produceDOMDocument
public Document produceDOMDocument()
throws ParserConfigurationException
{// use Sun's JAXP to create the DOM Document
DocumentBuilderFactory dbf =
DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = dbf.newDocumentBuilder();
Document doc = docBuilder.newDocument();
Element parent = null;
// create the root of the document
Element rootElement = doc.createElement("Customer");
doc.appendChild(rootElement);
addElement(doc, rootElement, "membership",
convertToString(getBean().getMembership()));
addElement(doc, rootElement, "firstName",
convertToString(getBean().getFirstName()));
addElement(doc, rootElement, "lastName",
convertToString(getBean().getLastName()));
addElement(doc, rootElement, "email",
convertToString(getBean().getEmail()));
return doc;

Java Bean

XML Data

JAXP

produceDOM Document

org.w3c.dom.Document