// create the xml model
CMNamedNodeMap nameNodeMap = cmDocument.getElements();
CMElementDeclaration cmElementDeclaration = (CMElementDeclaration) nameNodeMap.getNamedItem(getRootElementName());
Document xmlDocument = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
DOMContentBuilderImpl contentBuilder = new DOMContentBuilderImpl(xmlDocument);
// this 'uglyTempHack' flag is required in order to supress the
// creation a default encoding
// we'll handle this later in the domWriter.print() method used below
//
contentBuilder.supressCreationOfDoctypeAndXMLDeclaration = true;
contentBuilder.setBuildPolicy(buildPolicy);
contentBuilder.setOptionalElementDepthLimit(optionalElementDepthLimit);
contentBuilder.setExternalCMDocumentSupport(new MyExternalCMDocumentSupport(namespaceInfoList, xmlFileName));
contentBuilder.createDefaultRootContent(cmDocument, cmElementDeclaration, namespaceInfoList);
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputStream, charset);
DOMWriter domWriter = new DOMWriter(outputStreamWriter);