Class XML


  • public final class XML
    extends Object
    This class contains helper methods for creating XMLStreamReader and XMLStreamWriter objects.

    Creating a new XML stream reader:

    try (AutoCloseableXMLStreamReader xml = XML.reader(in)) { // Move XML stream to first element. xml.next(); return reader.read(xml); }
    Create a new XML stream reader:
    try (AutoCloseableXMLStreamWriter xml = XML.writer(out)) { writer.write(value, xml); }
    Create a new XML stream reader with pretty-print-indentation:
    final String indent = " "; try (AutoCloseableXMLStreamWriter xml = XML.writer(out, indent)) { writer.write(value, xml); }
    Since:
    3.9
    Version:
    3.9
    • Method Detail

      • reader

        public static AutoCloseableXMLStreamReader reader​(InputStream input)
                                                   throws XMLStreamException
        Create a new XML stream reader from the given input stream. The caller is responsible for closing the returned XMLStreamReader.
        try (AutoCloseableXMLStreamReader xml = XML.reader(in)) { // Move XML stream to first element. xml.next(); return reader.read(xml); }
        Parameters:
        input - the input stream
        Returns:
        a new Closeable XML stream reader
        Throws:
        XMLStreamException - if the creation of the XML stream reader fails
        NullPointerException - if the given input stream is null
      • writer

        public static AutoCloseableXMLStreamWriter writer​(OutputStream output,
                                                          String indent)
                                                   throws XMLStreamException
        Create a new XMLStreamWriter from the given output stream. The caller is responsible for closing the returned XMLStreamWriter.
        try (AutoCloseableXMLStreamWriter xml = XML.writer(out, " ")) { writer.write(value, xml); }
        Parameters:
        output - the underlying output stream
        indent - the element indent used for the XML output
        Returns:
        a new XMLStreamWriter instance
        Throws:
        XMLStreamException - if an error occurs while creating the XML stream writer
        NullPointerException - if the given output stream is null
      • writer

        public static AutoCloseableXMLStreamWriter writer​(OutputStream output)
                                                   throws XMLStreamException
        Create a new XMLStreamWriter from the given output stream. The caller is responsible for closing the returned XMLStreamWriter.
        try (AutoCloseableXMLStreamWriter xml = XML.writer(out)) { writer.write(value, xml); }
        Parameters:
        output - the underlying output stream
        Returns:
        a new XMLStreamWriter instance
        Throws:
        XMLStreamException - if an error occurs while creating the XML stream writer
        NullPointerException - if the given output stream is null