Class XMLSerializer

  • All Implemented Interfaces:
    DOMSerializer, Serializer, org.xml.sax.ContentHandler, org.xml.sax.DocumentHandler, org.xml.sax.DTDHandler, org.xml.sax.ext.DeclHandler, org.xml.sax.ext.LexicalHandler

    public class XMLSerializer
    extends BaseMarkupSerializer
    ASF Note: This class and dependencies were copied from the sun jdk1.5 source base. The original serializer is extended by a new OutputFormat setting that allows to break the line for each attribute. additionally, all attribute names are sorted alphabetically. Some Features were removed to limit the number of dependent classes:
    • dom filter support
    • all text nodes as CDATA feature
    • skip attribute default values feature
    • entity node reference feature

    Implements an XML serializer supporting both DOM and SAX pretty serializing. For usage instructions see Serializer.

    If an output stream is used, the encoding is taken from the output format (defaults to UTF-8). If a writer is used, make sure the writer uses the same encoding (if applies) as specified in the output format.

    The serializer supports both DOM and SAX. SAX serializing is done by firing SAX events and using the serializer as a document handler. DOM serializing is done by calling BaseMarkupSerializer.serialize(Document) or by using DOM Level 3

    If an I/O exception occurs while serializing, the serializer will not throw an exception directly, but only throw it at the end of serializing (either DOM or SAX's DocumentHandler.endDocument().

    For elements that are not specified as whitespace preserving, the serializer will potentially break long text lines at space boundaries, indent lines, and serialize elements on separate lines. Line terminators will be regarded as spaces, and spaces at beginning of line will be stripped.

    See Also:
    Serializer
    • Method Detail

      • setOutputFormat

        public void setOutputFormat​(OutputFormat format)
        Description copied from interface: Serializer
        Specifies an output format for this serializer. It the serializer has already been associated with an output format, it will switch to the new format. This method should not be called while the serializer is in the process of serializing a document.
        Specified by:
        setOutputFormat in interface Serializer
        Overrides:
        setOutputFormat in class BaseMarkupSerializer
        Parameters:
        format - The output format to use
      • setNamespaces

        public void setNamespaces​(boolean namespaces)
        This methods turns on namespace fixup algorithm during DOM serialization.
        Parameters:
        namespaces -
      • startElement

        public void startElement​(java.lang.String namespaceURI,
                                 java.lang.String localName,
                                 java.lang.String rawName,
                                 org.xml.sax.Attributes attrs)
                          throws org.xml.sax.SAXException
        Throws:
        org.xml.sax.SAXException
      • endElement

        public void endElement​(java.lang.String namespaceURI,
                               java.lang.String localName,
                               java.lang.String rawName)
                        throws org.xml.sax.SAXException
        Throws:
        org.xml.sax.SAXException
      • endElementIO

        public void endElementIO​(java.lang.String namespaceURI,
                                 java.lang.String localName,
                                 java.lang.String rawName)
                          throws java.io.IOException
        Throws:
        java.io.IOException
      • startElement

        public void startElement​(java.lang.String tagName,
                                 org.xml.sax.AttributeList attrs)
                          throws org.xml.sax.SAXException
        Throws:
        org.xml.sax.SAXException
      • endElement

        public void endElement​(java.lang.String tagName)
                        throws org.xml.sax.SAXException
        Throws:
        org.xml.sax.SAXException