- Functional Interface:
- This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.
XML writer interface, used for writing objects in XML format. The following
XML will show the marshaled representation of an
How to write the XML writing is shown by the next code snippet.
IntegerChromosome
.
<int-chromosome length="3">
<min>-2147483648</min>
<max>2147483647</max>
<alleles>
<allele>-1878762439</allele>
<allele>-957346595</allele>
<allele>-88668137</allele>
</alleles>
</int-chromosome>
The XML has been written by the following Writer
definition.
final Writer<IntegerChromosome> writer =
elem("int-chromosome",
attr("length").map(ch -> ch.length()),
elem("min", Writer.<Integer>text().map(ch -> ch.getMin())),
elem("max", Writer.<Integer>text().map(ch -> ch.getMax())),
elem("alleles",
elems("allele", Writer.<Integer>text())
.map(ch -> ISeq.of(ch).map(g -> g.getAllele()))
)
);
final IntegerChromosome ch = IntegerChromosome.of(MIN_VALUE, MAX_VALUE, 3);
try (AutoCloseableXMLStreamWriter xml = XML.writer(out, indent)) {
write(ch, xml);
}
- Since:
- 3.9
- Version:
- 3.9
-
Method Summary
Modifier and TypeMethodDescriptionstatic <T> Writer<T>
Writes the attribute with the givenname
to the current outer element.static <T> Writer<T>
Writes the attribute with the givenname
and a constantvalue
to the current outer element.static <T> Writer<T>
Adds an XML prolog element written by the givenwriter
.static <T> Writer<T>
Create a newWriter
, which writes an XML element with the given name and writes the given children into it.Creates a newWriter
, which writes the givenchildren
as sub-elements, defined by the givenchildWriter
.Creates a newWriter
, which writes the givenchildren
as sub-elements, defined by the givenchildWriter
.default <B> Writer<B>
Maps this writer to a different base type.static <T> Writer<T>
text()
Create a new textWriter
, which writes the given data as string to the outer element.void
write
(XMLStreamWriter xml, T data) Write the data of typeT
to the given XML stream writer.
-
Method Details
-
write
Write the data of typeT
to the given XML stream writer.- Parameters:
xml
- the underlyingXMLStreamWriter
, where the value is written todata
- the value to write- Throws:
XMLStreamException
- if writing the data failsNullPointerException
- if one of the arguments isnull
-
map
Maps this writer to a different base type. Mapping to a different data type is necessary when you are going to write sub-objects of your basic data typeT
. E.g. the chromosome length or themin
andmax
value of anIntegerChromosome
.- Type Parameters:
B
- the new data type of returned writer- Parameters:
mapper
- the mapper function- Returns:
- a writer with changed type
-
attr
Writes the attribute with the givenname
to the current outer element.final Writer<String> writer1 = elem("element", attr("attribute"));
- Type Parameters:
T
- the writer base type- Parameters:
name
- the attribute name- Returns:
- a new writer instance
- Throws:
NullPointerException
- if the attributename
isnull
- See Also:
-
attr
Writes the attribute with the givenname
and a constantvalue
to the current outer element.final Writer<MyObject> = elem("element", attr("version", "1.0"));
- Type Parameters:
T
- the writer base type- Parameters:
name
- the attribute namevalue
- the attribute value- Returns:
- a new writer instance
- Throws:
NullPointerException
- if one of thename
isnull
-
elem
Create a newWriter
, which writes an XML element with the given name and writes the given children into it.- Type Parameters:
T
- the writer base type- Parameters:
name
- the root element namechildren
- the XML child elements- Returns:
- a new writer instance
- Throws:
NullPointerException
- if one of the arguments isnull
-
text
Create a new textWriter
, which writes the given data as string to the outer element.- Type Parameters:
T
- the data type, which is written as string to the outer element- Returns:
- a new text writer
-
elems
Creates a newWriter
, which writes the givenchildren
as sub-elements, defined by the givenchildWriter
.- Type Parameters:
T
- the writer base type- Parameters:
name
- the enclosing element name used for each data valuewriter
- the sub-element writer- Returns:
- a new writer instance
- Throws:
NullPointerException
- if one of the arguments isnull
-
elems
Creates a newWriter
, which writes the givenchildren
as sub-elements, defined by the givenchildWriter
.- Type Parameters:
T
- the writer base type- Parameters:
writer
- the sub-element writer- Returns:
- a new writer instance
- Throws:
NullPointerException
- if one of the arguments isnull
-
doc
Adds an XML prolog element written by the givenwriter
. The default values for encoding and version are set to "UTF-8" and "1.0", respectively.<?xml version="1.0" encoding="UTF-8"?>
- Type Parameters:
T
- the writer data type- Parameters:
writer
- the root element writer- Returns:
- a new writer instance
-