pub fn write<'a>(
sink: impl Write,
root: impl Into<Root<'a>>,
) -> Result<(), Error>Expand description
Writes a publication or a message container as a DATEX II v3 XML document.
A publication becomes a d2:payload element, a MessageContainer
a con:messageContainer with exchange information and a con:payload per publication, each
with the namespace declarations, type attributes and element order the standard requires. When
the write succeeds the official schemas (3.7, and Exchange 2020 for a container) accept the
output, except for their uniqueness constraints on identifiers, which are not checked.
use datex2::v3::{self, common, situation::SituationPublication, Publication};
let publication = Publication::Situation(Box::new(SituationPublication {
lang: Some("en".into()),
publication_time: "2026-11-19T12:00:00Z".parse().ok(),
publication_creator: Some(Box::new(common::InternationalIdentifier {
country: Some("NL".into()),
national_identifier: Some("EXAMPLE".into()),
..Default::default()
})),
..Default::default()
}));
let xml = v3::to_string(&publication)?;
assert!(xml.contains("<com:country>NL</com:country>"));ยงErrors
The writer is strict. It fails, naming the element and the rule, when content the schema requires is absent, when a list has too few or too many entries, when a value breaks a length, range, digit or pattern rule, when a text holds a character XML cannot carry, when extension content is not well-formed, or when the sink fails. Bytes already written stay in the sink.
Examples found in repository?
3fn main() -> Result<(), Box<dyn std::error::Error>> {
4 let publication = Publication::Situation(Box::new(SituationPublication {
5 lang: Some("en".into()),
6 publication_time: Some("2026-11-19T12:00:00Z".parse()?),
7 publication_creator: Some(Box::new(common::InternationalIdentifier {
8 country: Some("NL".into()),
9 national_identifier: Some("EXAMPLE".into()),
10 ..Default::default()
11 })),
12 ..Default::default()
13 }));
14 v3::write(std::io::stdout().lock(), &publication)?;
15 Ok(())
16}More examples
13fn main() -> Result<(), Box<dyn std::error::Error>> {
14 let mut args: Vec<String> = std::env::args().skip(1).collect();
15 let format = match args.first().map(String::as_str) {
16 Some(f @ ("--json" | "--mobilithek")) => {
17 let f = f.to_owned();
18 args.remove(0);
19 f
20 }
21 _ => String::new(),
22 };
23 let path = args
24 .first()
25 .ok_or("usage: rewrite [--json | --mobilithek] <file>")?;
26 let input = BufReader::new(std::fs::File::open(path)?);
27 let doc = if path.ends_with(".json") {
28 json::read(input)?
29 } else {
30 v3::read(input)?
31 };
32 let mut out = BufWriter::new(std::io::stdout().lock());
33 for publication in &doc.publications {
34 match format.as_str() {
35 "--json" => json::write(&mut out, publication)?,
36 "--mobilithek" => json::mobilithek::write(&mut out, publication)?,
37 _ => v3::write(&mut out, publication)?,
38 }
39 }
40 out.flush()?;
41 Ok(())
42}