Start the XML stream document by defining things like the type of encoding, and prefixes used.
It needs to be used before calling any write method.

@throws ODataRenderException if unable to render
546565
public void startDocument() throws ODataRenderException {

        outputStream = new ByteArrayOutputStream();
        try {
            xmlWriter = XML_OUTPUT_FACTORY.createXMLStreamWriter(outputStream, UTF_8.name());
            entityTypeWriter = new MetadataDocumentEntityTypeWriter(xmlWriter, entityDataModel);
            complexTypeWriter = new MetadataDocumentComplexTypeWriter(xmlWriter, entityDataModel);
            enumTypeWriter = new MetadataDocumentEnumTypeWriter(xmlWriter);
            entitySetWriter = new MetadataDocumentEntitySetWriter(xmlWriter);
            singletonWriter = new MetadataDocumentSingletonWriter(xmlWriter);
            xmlWriter.writeStartDocument(UTF_8.name(), XML_VERSION);
            xmlWriter.setPrefix(EDMX_PREFIX, EDMX_NS);
        } catch (XMLStreamException e) {
            LOG.error("Not possible to start stream XML");
            throw new ODataRenderException("Not possible to start stream XML: ", e);
        }
    }
443137
@Override
    public void startDocument() throws SAXException {
        try {
            switch (doctype) {
                case NO_DOCTYPE:
                    return;
                case DOCTYPE_HTML5:
                    writer.write("<!DOCTYPE html>\n");
                    return;
                case DOCTYPE_HTML401_STRICT:
                    writer.write("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\" \"http://www.w3.org/TR/html4/strict.dtd\">\n");
                    return;
                case DOCTYPE_HTML401_TRANSITIONAL:
                    writer.write("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">\n");
                    return;
            }
        } catch (IOException ioe) {
            throw (SAXException)new SAXException(ioe).initCause(ioe);
        }
    }
877577
private void initDocumentParser() throws ParserConfigurationException {

    DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();

    docBuilderFactory.setNamespaceAware(false);
    docBuilderFactory.setValidating(true);

    docBuilderFactory.setAttribute(
        "http://java.sun.com/xml/jaxp/properties/schemaLanguage",
        "http://www.w3.org/2001/XMLSchema");

    docBuilderFactory.setAttribute(
        "http://java.sun.com/xml/jaxp/properties/schemaSource", resolveSchemaSource());

    docBuilder = docBuilderFactory.newDocumentBuilder();

    docBuilder.setErrorHandler(this);

    xpath = XPathFactory.newInstance().newXPath();
  }
1164163
public void build() throws IOException {
        if (! serialClassFoundToDocument(configuration.root.classes())) {
            //Nothing to document.
            return;
        }
        try {
            writer = configuration.getWriterFactory().getSerializedFormWriter();
            if (writer == null) {
                //Doclet does not support this output.
                return;
            }
        } catch (Exception e) {
            throw new DocletAbortException(e);
        }
        build(layoutParser.parseXML(NAME), contentTree);
        writer.close();
    }
1136165
@Override
    public final void xmlWriteRawOn(FormattedWriter writer, boolean callbackToItem) throws IOException
    {
        synchronized (_startLock)
        {
            if (_state == MessageStoreState.STATE_STARTED)
            {
                _xmlWriteRawOn(writer, callbackToItem);
            }
            else
            {
                writer.startTag(XML_MESSAGE_STORE);
                writer.indent();
                writer.newLine();
                writer.write(XML_CANNOT_BE_WRITTEN);
                writer.outdent();
                writer.newLine();
                writer.endTag(XML_MESSAGE_STORE);
            }
        }
    }
28432
public void xmlWriteOn(FormattedWriter writer) throws IOException
  {
    writer.newLine();
    writer.taggedValue("streamId", _streamId);
    writer.taggedValue("streamKey", _streamKey);
  }
1202200
public void startDocument(String encoding, Boolean standalone) throws IOException {
		@SuppressWarnings("unused")
		char apos = attributeUseApostrophe ? '\'' : '"';
		if (attributeUseApostrophe) {
			out.write("<?xml version='1.0'");
		} else {
			out.write("<?xml version=\"1.0\"");
		}
		if (encoding != null) {
			out.write(" encoding=");
			out.write(attributeUseApostrophe ? '\'' : '"');
			out.write(encoding);
			out.write(attributeUseApostrophe ? '\'' : '"');
			// out.write('\'');
		}
		if (standalone != null) {
			out.write(" standalone=");
			out.write(attributeUseApostrophe ? '\'' : '"');
			if (standalone.booleanValue()) {
				out.write("yes");
			} else {
				out.write("no");
			}
			out.write(attributeUseApostrophe ? '\'' : '"');
			// if(standalone.booleanValue()) {
			// out.write(" standalone='yes'");
			// } else {
			// out.write(" standalone='no'");
			// }
		}
		out.write("?>");
	}
978416
public void startDocument() throws SAXException
  {

    int doc = addNode(DTM.DOCUMENT_NODE,
                      DTM.DOCUMENT_NODE,
                      DTM.NULL, DTM.NULL, 0, true);

    m_parents.push(doc);
    m_previous = DTM.NULL;

    m_contextIndexes.push(m_prefixMappings.size());  // for the next element.
  }
207112
public void render(OutputStream outputStream, Format format, int dpi) throws PrintingException {
		try {
			if (baos == null) {
				prepare();
			}
			writeDocument(outputStream, format, dpi);
		} catch (Exception e) { // NOSONAR
			throw new PrintingException(e, PrintingException.DOCUMENT_RENDER_PROBLEM);
		}
	}
166025
@Override
    public ChunkedActionRenderResult renderStart(ODataRequestContext requestContext, QueryResult result,
                                                 OutputStream outputStream) throws ODataException {
        // Do nothing for default implementation
        return new ChunkedActionRenderResult(outputStream);
    }
129224
public void write(EntityType type) throws ODataRenderException {

        LOG.debug("Writing type {} of type {}", type.getName(), type.getMetaType());
        try {
            xmlWriter.writeStartElement(ENTITY_TYPE);
            if (!isNullOrEmpty(type.getBaseTypeName())) {
                xmlWriter.writeAttribute(BASE_TYPE, type.getBaseTypeName());
            }
            if (type.isOpen()) {
                xmlWriter.writeAttribute(OPEN_TYPE, Boolean.toString(type.isOpen()));
            }
            if (type.hasStream()) {
                xmlWriter.writeAttribute(HAS_STREAM, Boolean.toString(type.hasStream()));
            }
            xmlWriter.writeAttribute(NAME, type.getName());
            if (type.isAbstract()) {
                xmlWriter.writeAttribute(ABSTRACT, "true");
            }
            xmlWriter.writeStartElement(ENTITY_TYPE_KEY);
            for (PropertyRef propertyRef : type.getKey().getPropertyRefs()) {
                xmlWriter.writeStartElement(PROPERTY_REF);
                xmlWriter.writeAttribute(NAME, propertyRef.getPath());
                xmlWriter.writeEndElement();
            }
            xmlWriter.writeEndElement();

            visitProperties(entityDataModel, type, property -> {
                try {
                    propertyWriter.write(property);
                } catch (XMLStreamException e) {
                    throw new ODataRenderException("Error while writing property: " + property.getName(), e);
                }
            });
            xmlWriter.writeEndElement();
        } catch (XMLStreamException e) {
            throw new ODataRenderException("Error while writing entity type: " + type.getName(), e);
        }

    }
301468
@Override
    public void startElement(String uri, String localName, String name, Attributes attributes)
            throws SAXException {
        if ("table".equals(name)) {
            this.currentColumns = new LinkedHashMap<String, Column>();
            this.currentColumnTypes = new TreeMap<String, Integer>(String.CASE_INSENSITIVE_ORDER);
        } else if ("index".equals(name)) {
            this.currentIndex = new Index();
            this.currentIndex.setTable(this.currentTable);
        } else if ("unique".equals(name)) {
            this.currentUnique = new UniqueKey();
            this.currentUnique.setTable(this.currentTable);
        }

        this.chars = new StringBuilder();
    }
667474
@Override
    public void startPrefixMapping (String prefix, String uri) throws SAXException {
        if (elemLevel != 0) {
            getContentHandler().startPrefixMapping(prefix, uri);
        }
    }
276569
public String buildJson() throws ODataRenderException {
        LOG.debug("Start building Json service root document");
        try (ByteArrayOutputStream stream = new ByteArrayOutputStream()) {
            JsonGenerator jsonGenerator = JSON_FACTORY.createGenerator(stream, JsonEncoding.UTF8);
            jsonGenerator.writeStartObject();
            jsonGenerator.writeStringField(CONTEXT, getContextURL(uri, entityDataModel));
            jsonGenerator.writeArrayFieldStart(VALUE);


            List<EntitySet> entities = entityDataModel.getEntityContainer().getEntitySets();
            for (EntitySet entity : entities) {
                if (entity.isIncludedInServiceDocument()) {
                    writeObject(jsonGenerator, entity);
                }
            }

            List<Singleton> singletons = entityDataModel.getEntityContainer().getSingletons();
            for (Singleton singleton : singletons) {
                writeObject(jsonGenerator, singleton);
            }

            jsonGenerator.writeEndArray();
            jsonGenerator.writeEndObject();
            jsonGenerator.close();
            return stream.toString(StandardCharsets.UTF_8.name());
        } catch (IOException e) {
            throw new ODataRenderException("It is unable to render service document", e);
        }
    }
426617
public void writeStartDocument(String encoding, String version) throws FilterException
    {
        try {
            this.writer.writeStartDocument(encoding, version);
        } catch (XMLStreamException e) {
            throw new FilterException("Failed to write start document", e);
        }
    }
744166
private static void serializeDocToStream(final OutputStream output, final Document doc) throws IOException {
        final OutputFormat format = OutputFormat.createPrettyPrint();
        final XMLWriter writer = new XMLWriter(output, format);
        writer.write(doc);
        writer.flush();
    }
1063815
public void startDocument(OutputStream os) throws ODataRenderException {
        try {
            outputStream = os;
            xmlWriter = XML_OUTPUT_FACTORY.createXMLStreamWriter(os, UTF_8.name());
            metadataWriter = new AtomMetadataWriter(xmlWriter, oDataUri, entityDataModel, nsConfigurationProvider);
            dataWriter = new AtomDataWriter(xmlWriter, entityDataModel, nsConfigurationProvider);
            xmlWriter.writeStartDocument(UTF_8.name(), XML_VERSION);
            xmlWriter.setPrefix(METADATA, nsConfigurationProvider.getOdataMetadataNs());
            xmlWriter.setPrefix(ODATA_DATA, nsConfigurationProvider.getOdataDataNs());
        } catch (XMLStreamException e) {
            LOG.error("Not possible to start stream XML");
            throw new ODataRenderException("Not possible to start stream XML: ", e);
        }
    }