// Convert the metadata into an XML document
Document document = XMLPersistenceHelper.getEmptyDocument();
metadata.saveXML(document);
// Attach namespace and schema information to the document element
NamespaceInfo metadataNSInfo = CALFeatureMetadata.getNamespaceInfo();
XMLPersistenceHelper.attachNamespaceAndSchema(document, metadataNSInfo, MetadataPersistenceConstants.METADATA_SCHEMA_LOCATION, MetadataPersistenceConstants.METADATA_NS);
// Write the document to the metadata file
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(metadataOutputStream);
XMLPersistenceHelper.documentToXML(document, bufferedOutputStream, false);