* @throws HL7Exception if the data fields in the message do not permit encoding
* (e.g. required fields are null)
*/
protected String doEncode(Message source) throws HL7Exception {
if (source instanceof GenericMessage) {
throw new HL7Exception("Can't XML-encode a GenericMessage. Message must have a recognized structure.");
}
Document doc = encodeDocument(source);
Element documentElement = doc.getDocumentElement();
documentElement.setAttribute("xmlns", "urn:hl7-org:v2xml");
StringWriter out = new StringWriter();
OutputFormat outputFormat = new OutputFormat("", null, true);
outputFormat.setLineWidth(0);
if (textEncoding != null) {
outputFormat.setEncoding(textEncoding);
}
XMLSerializer ser = new XMLSerializer(out, outputFormat); //default output format
try {
ser.serialize(doc);
}
catch (IOException e) {
throw new HL7Exception(
"IOException serializing XML document to string",
HL7Exception.APPLICATION_INTERNAL_ERROR,
e);
}
return out.toString();