I am using the HL7 API 2.1 Beta (from today):
I am using the DefaultXmlParser to encode a Message Object to a XML String.
Doing so will call following method of ca.uhn.hl7v2.util.XMLUtils:
public static String serialize(Document document) {
DOMImplementationLS impl = getDOMImpl();
LSSerializer serializer = impl.createLSSerializer();
// document.normalizeDocument();
DOMConfiguration config = serializer.getDomConfig();
config.setParameter("xml-declaration", true);
config.setParameter("format-pretty-print", true);
LSOutput output = impl.createLSOutput();
output.setEncoding("UTF-8");
Writer writer = new StringWriter();
output.setCharacterStream(writer);
serializer.write(document, output);
return writer.toString();
}
The yellow marked line (config.setParameter("format-pretty-print", true);) will raise a DOMException:
org.w3c.dom.DOMException: FEATURE_NOT_SUPPORTED: The parameter format-pretty-print is recognized but the requested value cannot be set.
Will inquire about JDK version in use
Java 6 or Xerces 2.8.0 or newer is required. Sometimes there are more than one XML parser libs in the classpath due to transitive dependencies. Try to get rid of them.
I will add some checks to XmlUtils to fail fast in case of old parser libs.
Last edit: Christian Ohr 2013-02-01
A bit of research shows that pretty print isn't supported in JDK5. It's easy to check if it's supported though, so will add that. Also adding a config flag to disable pretty printing if desired.
Oops.. Hadn't refreshed and missed your comment Christian. :)
Closing per Christian's fix