code: @Override
    public void write(OutputStream out) throws IOException {
      try {
        valueSerializer = null;
  
        XMLStreamWriter serializer = factory.createXMLStreamWriter(out,
          "UTF-8");
        serializer.setPrefix(RequestConstants.RESTAPI_PREFIX,
          RequestConstants.RESTAPI_NS);
        serializer.setPrefix(RequestConstants.SEARCH_PREFIX,
          RequestConstants.SEARCH_NS);
        serializer.setPrefix("xsi",
          XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI);
        serializer.setPrefix("xs", XMLConstants.W3C_XML_SCHEMA_NS_URI);
  
        serializer.writeStartDocument("utf-8", "1.0");
  
        serializer.writeStartElement(RequestConstants.RESTAPI_PREFIX,
          "rule", RequestConstants.RESTAPI_NS);
  
        serializer.writeStartElement(RequestConstants.RESTAPI_PREFIX,
          "name", RequestConstants.RESTAPI_NS);
        serializer.writeCharacters(getName());
        serializer.writeEndElement();
  
        serializer.writeStartElement(RequestConstants.RESTAPI_PREFIX,
          "description", RequestConstants.RESTAPI_NS);
        String description = getDescription();
        if ( description != null ) serializer.writeCharacters(description);
        serializer.writeEndElement();
        serializer.flush();
  
        // logger.debug("Send: " + new String(queryPayload));
        XMLEventWriter eventWriter = factory.createXMLEventWriter(out);
        for (XMLEvent event : this.queryPayload) {
          eventWriter.add(event);
        }
        eventWriter.flush();
        out.flush();
  
        writeMetadataElement(serializer);
  
        serializer.writeEndElement();
        serializer.writeEndDocument();
      } catch (XMLStreamException e) {
        throw new MarkLogicIOException("Failed to serialize rule", e);
      } catch (TransformerFactoryConfigurationError e) {
        throw new MarkLogicIOException("Failed to serialize rule", e);
      } catch (TransformerException e) {
        throw new MarkLogicIOException("Failed to serialize rule", e);
      } finally {
        valueSerializer = null;
      }
  
    }
docs: Writes a serialized RuleDefinition to an OutputStream as XML.