FileDocCategorySizeDatePackage
PersonalDataXML.javaAPI DocExample2867Sun Sep 02 14:59:02 BST 2001chap6

PersonalDataXML

public class PersonalDataXML extends Object
Responsible for converting a PersonalData object into an XML representation using DOM.

Fields Summary
Constructors Summary
Methods Summary
private voidaddElem(org.w3c.dom.Document doc, org.w3c.dom.Element parent, java.lang.String elemName, java.lang.String elemValue, boolean required)
A helper method that simplifies this class.

param
doc the DOM Document, used as a factory for creating Elements.
param
parent the DOM Element to add the child to.
param
elemName the name of the XML element to create.
param
elemValue the text content of the new XML element.
param
required if true, insert 'required="true"' attribute.

        Element elem = doc.createElement(elemName);
        elem.appendChild(doc.createTextNode(elemValue));
        if (required) {
            elem.setAttribute("required", "true");
        }
        parent.appendChild(elem);
    
public org.w3c.dom.DocumentproduceDOMDocument(PersonalData personalData, boolean includeErrors)

param
personalData the data to convert to XML.
param
includeErrors if true, an extra field will be included in the XML, indicating that the browser should warn the user about required fields that are missing.
return
a DOM Document that contains the web page.


        // use Sun's JAXP to create the DOM Document
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder docBuilder = dbf.newDocumentBuilder();
        Document doc =  docBuilder.newDocument();

        // create <page>, the root of the document
        Element pageElem = doc.createElement("page");
        doc.appendChild(pageElem);

        // if needed, append <requiredFieldsMissing/>
        if (includeErrors && !personalData.isValid()) {
            pageElem.appendChild(doc.createElement(
                    "requiredFieldsMissing"));
        }

        Element personalDataElem = doc.createElement("personalData");
        pageElem.appendChild(personalDataElem);

        // use a private helper function to avoid some of DOM's
        // tedious code
        addElem(doc, personalDataElem, "firstName",
                personalData.getFirstName(), true);
        addElem(doc, personalDataElem, "lastName",
                personalData.getLastName(), true);
        addElem(doc, personalDataElem, "daytimePhone",
                personalData.getDaytimePhone(), true);
        addElem(doc, personalDataElem, "eveningPhone",
                personalData.getEveningPhone(), false);
        addElem(doc, personalDataElem, "email",
                personalData.getEmail(), true);

        return doc;