Class PlanitXmlWriterUtils


  • public class PlanitXmlWriterUtils
    extends Object
    Utilities for creating and using basic Java xml writers
    Author:
    markr
    • Constructor Detail

      • PlanitXmlWriterUtils

        public PlanitXmlWriterUtils()
    • Method Detail

      • createXMLWriter

        public static Pair<XMLStreamWriter,​Writer> createXMLWriter​(Path xmlFilePath)
        create an xml stream writer for the given path
        Parameters:
        xmlFilePath - to create the writer for
        Returns:
        created xml stream writer and writer instances
      • writeIndentation

        public static void writeIndentation​(XMLStreamWriter xmlWriter,
                                            int indentationLevel)
                                     throws XMLStreamException
        add indentation to stream at current indentation level via tabs
        Parameters:
        xmlWriter - to use
        indentationLevel - to use
        Throws:
        XMLStreamException - thrown if error
      • writeEmptyElement

        public static void writeEmptyElement​(XMLStreamWriter xmlWriter,
                                             String xmlElementName,
                                             int indentationLevel)
                                      throws XMLStreamException
        Write an empty element (with indentation), e.g. <xmlElementName>

        Requires a separate end element to be written

        Parameters:
        xmlWriter - to use
        xmlElementName - element to start tag, e.g. <xmlElementName>
        indentationLevel - to use
        Throws:
        XMLStreamException - thrown if error
      • writeStartElementNewLine

        public static void writeStartElementNewLine​(XMLStreamWriter xmlWriter,
                                                    String xmlElementName,
                                                    int indentationLevel)
                                             throws XMLStreamException
        Write a start element (with indentation) and add newline afterwards
        Parameters:
        xmlWriter - to use
        xmlElementName - element to start tag, e.g. <xmlElementName>
        indentationLevel - to use
        Throws:
        XMLStreamException - thrown if error
      • writeStartElement

        public static void writeStartElement​(XMLStreamWriter xmlWriter,
                                             String xmlElementName,
                                             int indentationLevel)
                                      throws XMLStreamException
        Write a start element (with indentation)
        Parameters:
        xmlWriter - to use
        xmlElementName - element to start tag, e.g. <xmlElementName>
        indentationLevel - to use
        Throws:
        XMLStreamException - thrown if error
      • writeElementWithCData

        public static void writeElementWithCData​(XMLStreamWriter xmlWriter,
                                                 String xmlElementName,
                                                 String elementCData,
                                                 int indentationLevel)
                                          throws XMLStreamException
        Write an element without attributes (with indentation) as well as its content and end element, e.g. <xmlElementName><![CDATA[elementCData]]></xmlElementName>
        Parameters:
        xmlWriter - to use
        xmlElementName - element to start tag, e.g. <xmlElementName>
        elementCData - to include
        indentationLevel - to use
        Throws:
        XMLStreamException - thrown if error
      • writeElementWithCDataNewLine

        public static void writeElementWithCDataNewLine​(XMLStreamWriter xmlWriter,
                                                        String xmlElementName,
                                                        String elementCData,
                                                        int indentationLevel)
                                                 throws XMLStreamException
        Write an element CDATA without attributes (with indentation) as well as its content and end element, e.g. <xmlElementName><![CDATA[elementCData]]></xmlElementName>. Add newline afterwards
        Parameters:
        xmlWriter - to use
        xmlElementName - element to start tag, e.g. <xmlElementName>
        elementCData - to include
        indentationLevel - to use
        Throws:
        XMLStreamException - thrown if error
      • writeElementWithValueWithNewLine

        public static void writeElementWithValueWithNewLine​(XMLStreamWriter xmlWriter,
                                                            String xmlElementName,
                                                            String elementValue,
                                                            int indentationLevel)
                                                     throws XMLStreamException
        Write an element without attributes (with indentation) as well as its content and end element, e.g. <xmlElementName>value</xmlElementName>. Add newline afterwards
        Parameters:
        xmlWriter - to use
        xmlElementName - element to start tag, e.g. <xmlElementName>
        elementValue - to include
        indentationLevel - to use
        Throws:
        XMLStreamException - thrown if error
      • writeElementWithValue

        public static void writeElementWithValue​(XMLStreamWriter xmlWriter,
                                                 String xmlElementName,
                                                 String elementValue,
                                                 int indentationLevel)
                                          throws XMLStreamException
        Write an element CDATA without attributes (with indentation) as well as its content and end element, e.g. <xmlElementName>value</xmlElementName>.
        Parameters:
        xmlWriter - to use
        xmlElementName - element to start tag, e.g. <xmlElementName>
        elementValue - to include
        indentationLevel - to use
        Throws:
        XMLStreamException - thrown if error
      • writeEndElementNewLine

        public static void writeEndElementNewLine​(XMLStreamWriter xmlWriter,
                                                  int indentationLevel)
                                           throws XMLStreamException
        Write an end element and add newline afterwards
        Parameters:
        xmlWriter - to use
        indentationLevel - to use
        Throws:
        XMLStreamException - thrown if error