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.

Java Bean

 

produceDOM Document

JAXP

XML Data

 

org.w3c.dom.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;

Chapter 10. Development of XML-based Enterprise applications 229

Page 245
Image 245
IBM Version 5 manual ProduceDOM Document, Org.w3c.dom.Document, Java Bean, XML Data