Class XMLSerializer

java.lang.Object
io.permazen.util.AbstractXMLStreaming
io.permazen.kv.util.XMLSerializer

public class XMLSerializer extends AbstractXMLStreaming
Utility methods for serializing and deserializing the contents of a KVStore to/from XML.

The XML has a simple format; empty values may be omitted:

  <xml version="1.0" encoding="UTF-8"?>

  <entries>
      <entry>
          <key>013f7b</key>
          <value>5502</value>
      </entry>
      <entry>
          <key>ee7698</key>
      </entry>
      ...
  </entries>
  
  • Field Details

    • ENTRIES_TAG

      public static final QName ENTRIES_TAG
    • ENTRY_TAG

      public static final QName ENTRY_TAG
    • KEY_TAG

      public static final QName KEY_TAG
    • VALUE_TAG

      public static final QName VALUE_TAG
  • Constructor Details

    • XMLSerializer

      public XMLSerializer(KVStore kv)
      Constructor.
      Parameters:
      kv - key/value store on which to operate
      Throws:
      IllegalArgumentException - if kv is null
  • Method Details

    • read

      public int read(InputStream input) throws XMLStreamException
      Import key/value pairs into the KVStore associated with this instance from the given XML input.

      The input is not closed by this method.

      Parameters:
      input - XML input
      Returns:
      the number of key/value pairs read
      Throws:
      XMLStreamException - if an error occurs
      IllegalArgumentException - if input is null
    • read

      public int read(XMLStreamReader reader) throws XMLStreamException
      Import key/value pairs into the KVStore associated with this instance from the given XML input. This method expects to see an opening <entries> as the next event (not counting whitespace, comments, etc.), which is then consumed up through the closing </entries> event. The reader is not closed by this method. Therefore, this tag could be part of a larger XML document.
      Parameters:
      reader - XML reader
      Returns:
      the number of key/value pairs read
      Throws:
      XMLStreamException - if an error occurs
      IllegalArgumentException - if reader is null
    • write

      public int write(OutputStream output, boolean indent) throws XMLStreamException
      Export all key/value pairs from the KVStore associated with this instance to the given output.

      The output is not closed by this method.

      Parameters:
      output - XML output; will not be closed by this method
      indent - true to indent output, false for all on one line
      Returns:
      the number of key/value pairs written
      Throws:
      XMLStreamException - if an error occurs
      IllegalArgumentException - if output is null
    • write

      public int write(Writer writer, boolean indent) throws XMLStreamException
      Export all key/value pairs from the KVStore associated with this instance to the given writer.

      The writer is not closed by this method.

      Parameters:
      writer - XML output; will not be closed by this method
      indent - true to indent output, false for all on one line
      Returns:
      the number of key/value pairs written
      Throws:
      XMLStreamException - if an error occurs
      IllegalArgumentException - if writer is null
    • write

      public int write(XMLStreamWriter writer, byte[] minKey, byte[] maxKey) throws XMLStreamException
      Export a range of key/value pairs from the KVStore associated with this instance to the given XML output.

      This method writes a start element as its first action, allowing the output to be embedded into a larger XML document. Callers not embedding the output may with to precede invocation of this method with a call to writer.writeStartDocument().

      The writer is not closed by this method.

      Parameters:
      writer - XML writer; will not be closed by this method
      minKey - minimum key (inclusive), or null for none
      maxKey - maximum key (exclusive), or null for none
      Returns:
      the number of key/value pairs written
      Throws:
      XMLStreamException - if an error occurs
      IllegalArgumentException - if writer is null