PersonalDataXMLpublic class PersonalDataXML extends Object Responsible for converting a PersonalData object into an XML
representation using DOM. |
Methods Summary |
---|
private void | addElem(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.
Element elem = doc.createElement(elemName);
elem.appendChild(doc.createTextNode(elemValue));
if (required) {
elem.setAttribute("required", "true");
}
parent.appendChild(elem);
| public org.w3c.dom.Document | produceDOMDocument(PersonalData personalData, boolean includeErrors)
// 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;
|
|