Skip to main content

Quick start: parsing

The examples read release.xml, any ERN 3.8.x, 4.2 or 4.3 message. To follow along, use one of the official DDEX samples in the repository, for example examples/Samples43/1 Audio.xml (CI runs every example on this page against that file).

Parse a message​

use ddex_parser::DDEXParser;
use std::io::Cursor;

fn main() -> Result<(), Box<dyn std::error::Error>> {
let xml = std::fs::read("release.xml")?;
let message = DDEXParser::new().parse(Cursor::new(xml))?;
let flat = &message.flat;

println!("{} ({}), {} release(s)", flat.message_id, flat.version, flat.releases.len());
for release in &flat.releases {
println!(
"{} by {}",
release.default_title.as_deref().unwrap_or("(no title)"),
release.display_artist.as_deref().unwrap_or("(no artist)"),
);
for track in &release.tracks {
println!(
" {}. {} [{}]",
track.position,
track.title.as_deref().unwrap_or("(no title)"),
track.isrc.as_deref().unwrap_or("no ISRC"),
);
}
}
Ok(())
}

parse takes any BufRead + Seek and returns a ParsedERNMessage with the flat model (message.flat) and the graph (message.graph).

For 1 Audio.xml the output starts like this:

Test1.1 (V4_3), 1 release(s)
Yume no Hajmari by Saeko Shu
1. Yume no Lullaby [JPTO09404900]

version is the version enum's name (V3_8_1, V3_8_2, V4_2, V4_3), not 4.3.

Release dates, labels, deals​

The flat model also carries release dates as written, genres, P and C lines, parental warnings, labels, the track layout and deals:

use ddex_parser::DDEXParser;
use std::io::Cursor;

fn main() -> Result<(), Box<dyn std::error::Error>> {
let message = DDEXParser::new().parse(Cursor::new(std::fs::read("release.xml")?))?;
let flat = &message.flat;

println!("control type: {:?}", flat.message_control_type);
for release in &flat.releases {
let dates: Vec<&str> = release.release_dates.iter().map(|d| d.date.as_str()).collect();
let genres: Vec<&str> = release.genres.iter().map(|g| g.genre.as_str()).collect();
println!("release dates {dates:?}, genres {genres:?}");
println!("label {:?}, parental warning {:?}", release.label_name, release.parental_warning);
if let Some(p_line) = &release.p_line {
println!("(P) {:?} {}", p_line.year, p_line.text);
}
for track in &release.tracks {
println!(
" disc {:?} track {:?}: {} enclosing group(s), track release {:?}",
track.disc_number,
track.track_number,
track.group_path.len(),
track.track_release_reference,
);
}
}
for deal in &flat.deals {
println!(
"deal for {:?} in {:?}, from {:?} to {:?}",
deal.releases, deal.territories.included, deal.validity.start, deal.validity.end
);
}
Ok(())
}

Every field is described in the parse output reference.

When parsing fails​

Input that is not XML, not a DDEX ERN message, or that breaks a security limit is an error: parse returns Err(ParseError) in Rust, throws an Error in Node.js and raises ValueError in Python. The message says what was wrong:

use ddex_parser::DDEXParser;
use std::io::Cursor;

fn main() {
let xml = "<catalog><item>not DDEX</item></catalog>";
match DDEXParser::new().parse(Cursor::new(xml)) {
Ok(_) => panic!("expected an error"),
Err(error) => println!("{error}"),
}
}

A message with a DTD (and so any external entity) is rejected before anything is read; see the security model.

Other parser functions​

  • Node.js: detectVersion(xml) returns the version ("V4_3"); sanityCheck(xml) resolves to { isValid, version, errors, warnings }; getDetailedError(xml) returns the parse error with its type and context. parse and parseSync take no options: every ParseOptions field throws since 0.5.0.
  • Python: detect_version(xml) returns "V4_3"; sanity_check(xml) returns a dict; stream(xml) parses the whole document and then yields one small dict per release (release_id, title, artist, track_count), so it does not reduce memory use.
  • Rust: DDEXParser::with_config(config) takes a ddex_parser::parser::security::SecurityConfig to change the limits. The streaming APIs (ParseMode::Stream, the stream* iterators) are experimental and read a subset of the fields; see Known issues and roadmap.