Package org.goplanit.utils.xml
Class PlanitXmlWriterUtils
- java.lang.Object
-
- org.goplanit.utils.xml.PlanitXmlWriterUtils
-
public class PlanitXmlWriterUtils extends Object
Utilities for creating and using basic Java xml writers- Author:
- markr
-
-
Constructor Summary
Constructors Constructor Description PlanitXmlWriterUtils()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static Pair<XMLStreamWriter,Writer>
createXMLWriter(Path xmlFilePath)
create an xml stream writer for the given pathstatic void
endXmlDocument(Pair<XMLStreamWriter,Writer> xmlFileWriterPair)
end the XML document and close the writers, streams etc.static void
startXmlDocument(XMLStreamWriter xmlWriter, String docType)
Start XML documentstatic void
writeElementWithCData(XMLStreamWriter xmlWriter, String xmlElementName, String elementCData, int indentationLevel)
Write an element without attributes (with indentation) as well as its content and end element, e.g.static void
writeElementWithCDataNewLine(XMLStreamWriter xmlWriter, String xmlElementName, String elementCData, int indentationLevel)
Write an element CDATA without attributes (with indentation) as well as its content and end element, e.g.static void
writeElementWithValue(XMLStreamWriter xmlWriter, String xmlElementName, String elementValue, int indentationLevel)
Write an element CDATA without attributes (with indentation) as well as its content and end element, e.g.static void
writeElementWithValueWithNewLine(XMLStreamWriter xmlWriter, String xmlElementName, String elementValue, int indentationLevel)
Write an element without attributes (with indentation) as well as its content and end element, e.g.static void
writeEmptyElement(XMLStreamWriter xmlWriter, String xmlElementName, int indentationLevel)
Write an empty element (with indentation), e.g.static void
writeEndElementNewLine(XMLStreamWriter xmlWriter, int indentationLevel)
Write an end element and add newline afterwardsstatic void
writeIndentation(XMLStreamWriter xmlWriter, int indentationLevel)
add indentation to stream at current indentation level via tabsstatic void
writeNewLine(XMLStreamWriter xmlWriter)
write a new line to the stream, e.g.static void
writeStartElement(XMLStreamWriter xmlWriter, String xmlElementName, int indentationLevel)
Write a start element (with indentation)static void
writeStartElementNewLine(XMLStreamWriter xmlWriter, String xmlElementName, int indentationLevel)
Write a start element (with indentation) and add newline afterwards
-
-
-
Field Detail
-
UTF8
public static final String UTF8
utf-8 string- See Also:
- Constant Field Values
-
XML_V1
public static final String XML_V1
xml versin 1.0 string- See Also:
- Constant Field Values
-
-
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
-
writeNewLine
public static void writeNewLine(XMLStreamWriter xmlWriter) throws XMLStreamException
write a new line to the stream, e.g. "\n"- Parameters:
xmlWriter
- to use- Throws:
XMLStreamException
- thrown if error
-
writeIndentation
public static void writeIndentation(XMLStreamWriter xmlWriter, int indentationLevel) throws XMLStreamException
add indentation to stream at current indentation level via tabs- Parameters:
xmlWriter
- to useindentationLevel
- 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 usexmlElementName
- 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 usexmlElementName
- 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 usexmlElementName
- 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 usexmlElementName
- element to start tag, e.g.<xmlElementName>
elementCData
- to includeindentationLevel
- 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 usexmlElementName
- element to start tag, e.g.<xmlElementName>
elementCData
- to includeindentationLevel
- 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 usexmlElementName
- element to start tag, e.g.<xmlElementName>
elementValue
- to includeindentationLevel
- 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 usexmlElementName
- element to start tag, e.g.<xmlElementName>
elementValue
- to includeindentationLevel
- 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 useindentationLevel
- to use- Throws:
XMLStreamException
- thrown if error
-
startXmlDocument
public static void startXmlDocument(XMLStreamWriter xmlWriter, String docType) throws XMLStreamException
Start XML document- Parameters:
xmlWriter
- the writerdocType
- to reference- Throws:
XMLStreamException
- thrown if exception
-
endXmlDocument
public static void endXmlDocument(Pair<XMLStreamWriter,Writer> xmlFileWriterPair) throws XMLStreamException, IOException
end the XML document and close the writers, streams etc.- Parameters:
xmlFileWriterPair
- writer pair- Throws:
XMLStreamException
- thrown if errorIOException
- thrown if error
-
-